Click here to Skip to main content
12,395,645 members (62,297 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# WPF
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
Posted 10-Jan-12 21:42pm
Updated 10-Jan-12 21: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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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];
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
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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:
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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);

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 11 Jun 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100