c# - 如何将ImageBrush转换为图像

标签 c# wpf

我创建了一个绘画 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>

现在,在我完成绘制后,我想将其保存到文件中 + 希望将其转换为 c# 代码中的 pictureBox 或图像或位图

我该怎么做?

已经尝试过

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

我在 StackOverFlow 和 google 中找到的所有内容 是从 Image 转换为 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);
}

关于c# - 如何将ImageBrush转换为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39851568/

相关文章:

c# - 找到标志枚举长度的有效方法?

c# - wpf datagrid - 如何删除黑色选定边框?

c# - XAML ListBox 只显示类名

javascript - 如何在mvc 5中基于静态下拉列表过滤数据库记录

c# - 处理 Quartz.NET 的正确方法?

wpf - 无需额外线程即可防止 UI 卡住

c# - 附加文本时如何防止文本框自动滚动?

wpf - 如何让ContentControl解析DataTemplate

c# - 显示为空的静态列表

c# - 我如何改进这个价格计算代码,现在进行 4 次计算,但在不久的将来会进行更多计算?