c#-将inkCanvas中的图像另存为png或jpeg文件
作者:互联网
这是我的WPF代码
< InkCanvas x:Name =“ inkCanvas” Margin =“ 9,325,210,193” Background =“ Azure”>< / InkCanvas>
还有一个按钮
当按下按钮时,我想将绘制的图像保存到文件中.这是我的代码
private void button1_Click(object sender, RoutedEventArgs e)
{
int margin = (int)inkCanvas.Margin.Left;
int width = (int)inkCanvas.ActualWidth - margin;
int height = (int)inkCanvas.ActualHeight - margin;
RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
rtb.Render(inkCanvas);
using (FileStream fs = new FileStream("path", FileMode.Create))
{
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(rtb));
encoder.Save(fs);
}
}
但是,如果用油漆打开,则显示的图像全是黑色(从浏览器中掠过)或白色.
我该怎么做才能完全获得绘制的图像? ty.
解决方法:
问题是您试图将矢量图形另存为位图,那是不可能的,因此首先需要绘制矢量,然后保存图形
此类将Ink绘制到现有位图上
public class InkImage
{
public static BitmapFrame MergeInk(StrokeCollection ink, BitmapSource background)
{
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
drawingContext.DrawImage(background, new Rect(0, 0, background.Width, background.Height));
foreach (var item in ink)
{
item.Draw(drawingContext);
}
drawingContext.Close();
var bitmap = new RenderTargetBitmap((int)background.Width, (int)background.Height, background.DpiX, background.DpiY, PixelFormats.Pbgra32);
bitmap.Render(drawingVisual);
return BitmapFrame.Create(bitmap);
}
}
}
然后您可以使用JPEG或PNG编码器保存位图
标签:png,save,inkcanvas,wpf,c 来源: https://codeday.me/bug/20191026/1936759.html