我有3D WPF Visual我想进入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.Image 或a System.Windows.Media.Imaging.RenderTargetBitmap;只有一个 System.Drawing.Image.

我如何转换 Controls.Image.Imaging.RenderTargetBitmap 进入 Image, ,还是有任何简单的方法?

有帮助吗?

解决方案 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);

其他提示

您可以从 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);

也许我不正确理解这个问题,但是您想将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)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top