c# - 在 C# 中将位图转换为 WriteableBitmap

标签 c# wpf

<分区>

我在我的应用程序中使用 C#。我已经直接将 Bitmap 转换为 WritableBitmap。 我不想将位图转换为其他格式。

    pdftron.PDF.PDFDraw draw = new pdftron.PDF.PDFDraw();
        using (System.Drawing.Bitmap gdiBitmap = draw.GetBitmap(_pdfDoc.GetPage(page)))
        {

            // System.Drawing.Bitmap myBitmap = BitmapFromWriteableBitmap(target);
            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(gdiBitmap))
            {
                System.Drawing.Color customColor = System.Drawing.Color.FromArgb(50, System.Drawing.Color.Red);
                System.Drawing.SolidBrush shadowBrush = new System.Drawing.SolidBrush(customColor);
                g.FillRectangles(shadowBrush, new System.Drawing.Rectangle[] { rectangle });
            }
}

现在我必须在 WritableBitmap 中转换它。

最佳答案

首先,您需要从您的Bitmap 创建BitmapSource,然后您可以从中创建WriteableBitmap

System.Windows.Media.Imaging.BitmapSource bitmapSource =
  System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
  gdiBitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
  System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
System.Windows.Media.Imaging.WriteableBitmap writeableBitmap = new System.Windows.Media.Imaging.WriteableBitmap(bitmapSource);

关于c# - 在 C# 中将位图转换为 WriteableBitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31807053/

相关文章:

c# - UI 中等待任务的问题

C# WPF 拉伸(stretch)/缩放窗口与大小成比例

c# - 在C#项目中使用C++ DLL

WPF改变亮度

c# - 如何编写使用从 C# 创建的临时表的存储过程

c# - Microsoft Word if 带有合并字段的语句不起作用

.net - 反转 ListBox 项目(按 "nothing"降序排序)

wpf - 使用 ScatterView 在 MVVM 中拖放

c# - 在 asp.net 网站中动态添加项目到菜单

C# 和 Razor - 您请求的页面类型未提供,因为它已被明确禁止。扩展名 '.cshtml' 可能不正确