在C#中将位图转换为可写位图

3

我在我的应用程序中使用c#。我想直接将位图转换为可写位图,而不是将位图转换为其他格式。

    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)。
1个回答

4

首先,您需要从您的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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接