Click here to Skip to main content
15,566,994 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Convert image to Bitmap Image.
Am using visual studio 2010 c# (WPF).
I got this error
Can not implicitly convert System.Windows.Media.ImageSource to

How to convert it to one type to other.
help me
Updated 10-Jan-12 22:44pm
vlad781 11-Jun-12 12:57pm    
You have posted us an error you are getting. We cannot provide you with a proper solution unless you tell us how you are invoking the method (button click)? See my answer below.

If you have a byte array that represents a file that WPF can decode (bmp, jpg, gif, png, tif, ico), you can do the following
BitmapSource LoadImage(Byte[] imageData)
    using (MemoryStream ms = new MemoryStream(imageData))
        var decoder = BitmapDecoder.Create(ms,
            BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
        return decoder.Frames[0];
Share this answer
ranjith m amin 17-Jan-12 23:58pm    
Sir can you explain it plz... I did not understood how to implement it in my code ... I am using Image control in my design page source of that i need in BitmapImage control that i defining in code page... Please help me sir
The following articles contain useful information about converting ImageSource to BitmapImage and Bitmap:

There's also a solution that uses a third party toolkit here:
Share this answer
Telep: a rich text box
On a button click, I was taking the rich text box creating a bitmap, and then making it in the same location as telep, but above it. And then I finally made it mirrored horizontally.

public void pictureBox6_Click(object sender, EventArgs e)
            //Create a PictureBox with the same location and size as the RichTextBox.
            PictureBox pic = new PictureBox();
            pic.Location = this.telep.Location;
            pic.Size = this.telep.Size;

            //Draw the richTextBox to a bitmap
            Bitmap bitmap = new Bitmap(this.telep.Width, this.telep.Height);
            DrawToBitmap(this.telep, bitmap, new Rectangle(Point.Empty, this.telep.Size));

            //Set the image of the PictureBox to that image.
            pic.Image = bitmap;

            pic.Dock = DockStyle.Fill;

        private void DrawToBitmap(RichTextBox richTextBox, Bitmap image, Rectangle targetRectangle)
            Graphics g = telep.CreateGraphics();
            Graphics g2 = Graphics.FromImage(image);
            IntPtr gi = g.GetHdc();
            IntPtr gi2 = g2.GetHdc();
            BitBlt(gi2, 0, 0, richTextBox.Width, richTextBox.Height, gi, 0, 0, 0x00CC0020);

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900