Link to home
Create AccountLog in
Avatar of RTSol
RTSol

asked on

Image control to Bitmap in WPF

Hi,

I have a WPF application with a Image control. How can I get the image in this control converted to a bitmap - where som work is done - and then move it back to the Image control again? I am working in C# in Visual Studio 2008 SP1.

Nest regards
RTSol
Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

http://www.codeproject.com/KB/WPF/ImageCropper.aspx - WPF Interactive Image Cropping Control
Avatar of RTSol
RTSol

ASKER

Hi,

In this code it seems that the image has to be present in the file system. What if it is not - how do you get the image from the image control into a bitmap?

-RTSol
ASKER CERTIFIED SOLUTION
Avatar of k_swapnil
k_swapnil
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of RTSol

ASKER

Hi,

This is probably what I want. I need an additinal line of code though - Se the snippet. Can you help me here?

Best regards
RTSol
            int imW = (int)scannedImage.ActualWidth; //scannedImage is my System.Windows.Controls.Image
            int imH = (int)scannedImage.ActualHeight;

            RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(imW, imH, 100, 100, PixelFormats.Default);
            renderTargetBitmap.Render(scannedImage); // Your image object

            PngBitmapEncoder pngBitmapEncoder = new PngBitmapEncoder();
            pngBitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));

            System.Drawing.Image im2 =   //Here I want to convert to a System.Drawing.Image

            makePDF("c:\\testPDF.pdf", im2); //A method I have that takes two arguments - path and System.Drawing.Image

Open in new window

Avatar of RTSol

ASKER

Hi again,

I solved it!

            MemoryStream stream = new MemoryStream();
            pngBitmapEncoder.Save(stream);
            System.Drawing.Image im2 = System.Drawing.Image.FromStream(stream);

Thanks a lot!
RTSol