tags:

views:

48

answers:

2

I have a project which requires be to convert an in memory System.Drawing.Bitmap into an in memory BitmapImage so I can bind the BitmapImage to an Image control via XAML. The problem I am running into is during the conversion the original bitmap gets shrunk and is very blurry.

This is very bad because the original bitmap is a bar code and I need this bar code to stay readable. Is there anything I can do to preserve the integrity of my bitmap? Or is it possible to bind a System.Drawing.Bitmap to an WPF Image control without first saving the bitmap and using a URI?

Also can anyone explain to me the difference between all these image formats? It seems as if there are a ton of them, they reside in numerous namespaces and it is a pain to convert between them.

EDIT.....

public static BitmapImage GetBitmapImage(Bitmap bmp)
    {
        MemoryStream ms = new MemoryStream();
        bmp.Save(ms, ImageFormat.Bmp);

        BitmapImage bmpI = new BitmapImage();
        bmpI.BeginInit();
        bmpI.StreamSource = ms;
        bmpI.EndInit();

        ms.Close();
        ms.Dispose();

        return bmpI;
    }
+1  A: 

Have you tried different bitmap scaling modes?

For example:

Image RenderOptions.BitmapScalingMode="HighQuality" ...

Jeremiah Morrill
No, I will give that a try. Will that prevent the bitmap from being scaled downwards when it is converted?
The BitmapScalingMode is the resizing algorithm used when the image is scaled up or down.
Jeremiah Morrill
I finally got around to messing with this and I couldn't get it fix anything. Where should this be implemented at?
+1  A: 

In my opinion, the easiest way to get rid of all those burdens is to create a user control, then put a

WindowsFormsHost

on to that control. After that you put a windows forms image to the host, and in the underlying code of the user control, you can create a dependency property to bind data and update changes to the image control.

Cheers.

DrakeVN
would you use a picture box as the control in the host?
Yes you can add a picture box to the host.
DrakeVN
So I got this wired up and it works but when I render the parent control containing the windowsformhost to a bitmap the windowsformhost and associated child bitmap is not captured. Is this because windowsformshost is not a dependencyobject?
Can you please elaborate "I render the parent control containing the windowsformhost to a bitmap".Why do you need to render a control to a bitmap. You can add directly the user control which contains the WindowsFormsHost to the interface.Besides, to make the user control support binding, you can create a dependency property which takes a Bitmap as the type.
DrakeVN
I have to render the parent as a bitmap because I am treating the parent control as a container for a label, which will be printed. The bitmap problem I having surrounds one child element which is bar code. I made the dependency property but I can't figure how to bind it the Image Property of picture box.