.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > Converting DrawingVisual to BitmapSource
 

Converting DrawingVisual to BitmapSource

I have a function that takes a list of bitmap image and merge them. It returns the merged image as DrawingImage type. I have used the drawing image as source for the Image control.

Now what i want to do is to convert the drawing image to bitmapsource type.
Can anyone provide me with the sample code for this.?
Thank you.
Biprajiman
Thank you for the reply but i also solved it with these lines of code. Don't know if this is effective method but it is solved.

DrawingImage _returnObject = MergeImage(tempListBitmap);//returns DrawingImage

if (_returnObject != null)
{
_returnObject.Freeze();

System.Windows.Controls.Image _testImage = new System.Windows.Controls.Image();

_testImage.Source = _returnObject;

DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
drawingContext.DrawImage(_testImage.Source,new Rect(0,0,_testImage.Source.Width,_testImage.Source.Height));
drawingContext.Close();

RenderTargetBitmap bmp = new RenderTargetBitmap((int)_testImage.Source.Width, (int)_testImage.Source.Height, 96, 96, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);

bmp.Render(_testImage);

imgTest.Source = bmp;
imgTest.Visibility = Visibility.Visible;
lsImages.Visibility = Visibility.Hidden;

_objWindow3.PBmpSource = (BitmapSource)bmp;

}
  • Marked As Answer byBiprajiman Wednesday, September 23, 2009 12:05 PM
  •  
Biprajiman
Hi, you shoud use the method

RenderTargetBitmap bmp = new RenderTargetBitmap((Int32)Math.Ceiling(width),

(Int32)Math.Ceiling(height),

(Double)DeviceHelper.PixelsPerInch(Orientation.Horizontal),

(Double)DeviceHelper.PixelsPerInch(Orientation.Vertical),

PixelFormats.Pbgra32);
DrawingVisual dv = new DrawingVisual();
..................................
bmp.Render(dv);

bmp is a BitMapSource





Hope this help you

internal class DeviceHelper

{

public static Int32 PixelsPerInch(Orientation orientation)

{

Int32 capIndex = (orientation == Orientation.Horizontal) ? 0x58 : 90;

using (DCSafeHandle handle = UnsafeNativeMethods.CreateDC("DISPLAY"))

{

return (handle.IsInvalid ? 0x60 : UnsafeNativeMethods.GetDeviceCaps(handle, capIndex));

}

}

}

[DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]

public static extern Int32 GetDeviceCaps(DCSafeHandle hDC, Int32 nIndex);

[DllImport("gdi32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]

public static extern Boolean DeleteDC(IntPtr hDC);

internal sealed class DCSafeHandle : SafeHandleZeroOrMinusOneIsInvalid

{

private DCSafeHandle() : base(true) { }

protected override Boolean ReleaseHandle()

{

return UnsafeNativeMethods.DeleteDC(base.handle);

}

}


Oscar Avarez Guerras - Arquitecto Software en I3B (I+D+I) Blog:http://geeks.ms/blogs/oalvarez Por favor marca como respuesta si te ha ayudado esta respuesta
bialguos
Thank you for the reply but i also solved it with these lines of code. Don't know if this is effective method but it is solved.

DrawingImage _returnObject = MergeImage(tempListBitmap);//returns DrawingImage

if (_returnObject != null)
{
_returnObject.Freeze();

System.Windows.Controls.Image _testImage = new System.Windows.Controls.Image();

_testImage.Source = _returnObject;

DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
drawingContext.DrawImage(_testImage.Source,new Rect(0,0,_testImage.Source.Width,_testImage.Source.Height));
drawingContext.Close();

RenderTargetBitmap bmp = new RenderTargetBitmap((int)_testImage.Source.Width, (int)_testImage.Source.Height, 96, 96, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);

bmp.Render(_testImage);

imgTest.Source = bmp;
imgTest.Visibility = Visibility.Visible;
lsImages.Visibility = Visibility.Hidden;

_objWindow3.PBmpSource = (BitmapSource)bmp;

}
  • Marked As Answer byBiprajiman Wednesday, September 23, 2009 12:05 PM
  •  
Biprajiman

You can use google to search for other answers

Custom Search

More Threads

• combo box DropDown List not working
• NotifyIcon
• Is there a way to show a window unactivated in .NET 3.0 SP0 ?
• Binding
• Zoom object on window
• How can I create a storyboard in code like this but in WPF?
• GridView styling
• GetPositionAtOffset() strange behavior
• WPF and Windows Forms rendering problem
• Using GuidelineSet with DrawingContext