如何将ImageBrush转换为图像

本文关键字:图像 转换 ImageBrush | 更新日期: 2023-09-27 18:11:22

我创建了一个彩色画布

<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>

在我完成绘图之后我想把它保存到文件+想要在c#代码中将它转换为pictureBox或Image或bitmap

我该怎么做?

已经试过

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

我在StackOverFlow和google上找到的所有东西是从Image转换为imageBrush

提前感谢

如何将ImageBrush转换为图像

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);
}

试试下面的代码。它可能对你有帮助。

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;
        }