Convert RenderTargetBitmap into System.Drawing.Image
Asked Answered
P

3

6

I have 3D WPF visual that I want to pass into an Excel cell (via clipboard buffer).

With "normal" BMP images it works but I do not know how to convert a RenderTargetBitmap.

My code looks like this:

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

My problem is that gr.DrawImage does not accept a System.Windows.Controls.Image or a System.Windows.Media.Imaging.RenderTargetBitmap; only a System.Drawing.Image.

How do I convert the Controls.Image.Imaging.RenderTargetBitmap into an Image, or are there any easier ways?

Pudendas answered 18/10, 2011 at 14:17 Comment(0)
P
2

This was the solution I came up with

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);
Pudendas answered 19/10, 2011 at 13:4 Comment(0)
T
6

You can copy the pixels from the RenderTargetBitmap directly into the pixel buffer of a new Bitmap. Note that I've assumed that your RenderTargetBitmap uses PixelFormats.Pbrga32, as use of any other pixel format will throw an exception from the constructor of 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);
Tippets answered 28/2, 2014 at 5:38 Comment(0)
P
2

This was the solution I came up with

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);
Pudendas answered 19/10, 2011 at 13:4 Comment(0)
G
0

Maybe I don't understand the question right, but you want to copy a RenderTargetBitmap to the clipboard, couldn't you just call 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)
Grab answered 19/2, 2013 at 22:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.