电子白板绘制的内容
点击 保存 按钮生成的图片
Inkcanvas 绘制后保存图片(jpe、png)没有背景的色,下面代码实现添加背景色:
1、BitmapSource 扩展方法,给图片添加背景色
public static BitmapSource ReplaceTransparency(this BitmapSource bitmap, Color color)
{
Rect rect = new Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight);
DrawingVisual visual = new DrawingVisual();
DrawingContext context = visual.RenderOpen();
context.DrawRectangle(new SolidColorBrush(color), null, rect);
context.DrawImage(bitmap, rect);
context.Close();
RenderTargetBitmap render = new RenderTargetBitmap(bitmap.PixelWidth, bitmap.PixelHeight, 96, 96, PixelFormats.Pbgra32);
render.Render(visual);
return render;
}
2、保存图片
private void SaveImage(InkCanvas inkCanvas,string path)
{
RenderTargetBitmap rtb = new RenderTargetBitmap((int)inkCanvas.ActualWidth, (int)inkCanvas.ActualHeight, 96d, 96d, PixelFormats.Default);
rtb.Render(inkCanvas);
//添加白色背景
BitmapSource bitmapSource = BitmapSourceExtensions.ReplaceTransparency(rtb, Colors.White);
JpegBitmapEncoder enc = new JpegBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapSource));
using (FileStream fs = new FileStream(path, FileMode.Create))
{
enc.Save(fs);
}
}