【问题标题】:Convert RenderTargetBitmap into System.Drawing.Image将 RenderTargetBitmap 转换为 System.Drawing.Image
【发布时间】:2011-10-18 14:17:08
【问题描述】:

我有 3D WPF 视觉对象,我想将其传递到 Excel 单元格(通过剪贴板缓冲区)。

使用“普通”BMP 图像它可以工作,但我不知道如何转换 RenderTargetBitmap

我的代码如下所示:

System.Windows.Media.Imaging.RenderTargetBitmap renderTarget = myParent.GetViewPortAsImage(DiagramSizeX, DiagramSizeY);
System.Windows.Controls.Image myImage = new System.Windows.Controls.Image();
myImage.Source = renderTarget;

System.Drawing.Bitmap pg = new System.Drawing.Bitmap(DiagramSizeX, DiagramSizeY);
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(pg);
gr.DrawImage(myImage, 0, 0);

System.Windows.Forms.Clipboard.SetDataObject(pg, true);
sheet.Paste(range);

我的问题是gr.DrawImage 不接受System.Windows.Controls.ImageSystem.Windows.Media.Imaging.RenderTargetBitmap;只有System.Drawing.Image

如何将Controls.Image.Imaging.RenderTargetBitmap 转换为Image,或者有更简单的方法吗?

【问题讨论】:

    标签: c# wpf system.drawing bitmapimage rendertargetbitmap


    【解决方案1】:

    您可以将RenderTargetBitmap 中的像素直接复制到新Bitmap 的像素缓冲区中。请注意,我假设您的RenderTargetBitmap 使用PixelFormats.Pbrga32,因为使用任何其他像素格式都会从RenderTargetBitmap 的构造函数中引发异常。

    var bitmap = new Bitmap(renderTarget.PixelWidth, renderTarget.PixelHeight,
        PixelFormat.Format32bppPArgb);
    
    var bitmapData = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size),
        ImageLockMode.WriteOnly, bitmap.PixelFormat);
    
    renderTarget.CopyPixels(Int32Rect.Empty, bitmapData.Scan0,
        bitmapData.Stride*bitmapData.Height, bitmapData.Stride);
    
    bitmap.UnlockBits(bitmapData);
    

    【讨论】:

      【解决方案2】:

      这是我想出的解决方案

      System.Windows.Media.Imaging.RenderTargetBitmap renderTarget = myParent.GetViewPortAsImage(DiagramSizeX, DiagramSizeY);
      System.Windows.Media.Imaging.BitmapEncoder encoder = new System.Windows.Media.Imaging.PngBitmapEncoder();
      MemoryStream myStream = new MemoryStream();
      
      encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(renderTarget));
      encoder.Save(myStream);
      //
      System.Drawing.Bitmap pg = new System.Drawing.Bitmap(DiagramSizeX, DiagramSizeY);
      System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(pg);
      //
      // Background
      //
      gr.FillRectangle(new System.Drawing.SolidBrush(BKGC), 0, 0, DiagramSizeX, DiagramSizeY);
      //
      gr.DrawImage(System.Drawing.Bitmap.FromStream(myStream), 0, 0);
      System.Windows.Forms.Clipboard.SetDataObject(pg, true);
      
      sheet.Paste(range);
      

      【讨论】:

        【解决方案3】:

        也许我没听懂这个问题,但是你想复制一个 RenderTargetBitmap 到剪贴板,你不能直接调用 SetImage 吗?:

            Dim iRT As RenderTargetBitmap = makeImage() //this is what you do to get the rendertargetbitmap
            If iRT Is Nothing Then Exit Sub
            Clipboard.SetImage(iRT)
        

        【讨论】:

          猜你喜欢
          • 2012-12-08
          • 2011-04-14
          • 2011-01-25
          • 1970-01-01
          • 2023-03-22
          • 2020-02-09
          • 1970-01-01
          • 2015-11-02
          • 2012-02-14
          相关资源
          最近更新 更多