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
Microsoft DevelopmentImages and PhotosC#

Avatar of undefined
Last Comment
RTSol

8/22/2022 - Mon
Roshan Davis

http://www.codeproject.com/KB/WPF/ImageCropper.aspx - WPF Interactive Image Cropping Control
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
k_swapnil

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
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