WPF Inkcanvas 电子白板保存图片添加背景色

电子白板绘制的内容

点击 保存 按钮生成的图片

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

}

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值