Asaf Shazar
Asaf Shazar

Reputation: 1065

How to convert ImageBrush to image

I Created a Painted canvas

<Canvas Grid.Row="1" Name="PaintCanvas" MouseDown="PaintCanvas_MouseDown" MouseUp="PaintCanvas_MouseUp" MouseMove="PaintCanvas_MouseMove">
        <Canvas.Background>
            <ImageBrush ImageSource="/MyNoteBook;component/Images/LinnedPage.png"/>
        </Canvas.Background>

now after I finish to paint on it I want to save it to File + Want to convert it to pictureBox or Image or bitmap in c# code

How I do that?

Already tried

ImageBrush picture = (ImageBrush)PaintCanvas.Background;
Image a = (Image)picture;
System.Drawing.Bitmap btmap = (System.Drawing.Bitmap)picture;

All things i found on StackOverFlow and in google Is convert from Image to imageBrush

Thank you in advance

Upvotes: 2

Views: 2778

Answers (2)

AnjumSKhan
AnjumSKhan

Reputation: 9827

ImageBrush b =  (ImageBrush)PaintCanvas.Background;
BitmapSource src = (BitmapSource)b.ImageSource;

string path = @"g:\myimg-name.jpg";
using (FileStream fs1 = new FileStream(path, FileMode.OpenOrCreate))
{
    BitmapFrame frame = BitmapFrame.Create(src);

    JpegBitmapEncoder enc = new JpegBitmapEncoder();
    enc.Frames.Add(frame);
    enc.Save(fs1);
}

Upvotes: 2

Sneha Patel
Sneha Patel

Reputation: 393

Try following code. It may helps you.

if (((Grid)sender).Children.Count > 0)
        {
            gridBackground = (ImageBrush)(((Grid)sender).Background);
            System.Windows.Controls.Image gridBackImage = new System.Windows.Controls.Image();
            gridBackImage.Source = gridBackground.ImageSource;

            ImageCar.Source = gridBackImage.Source;
        }

Upvotes: 2

Related Questions