Click here to Skip to main content
Rate this: bad
good
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
System.Windows.Media.Imaging.BitmapImage.
How to convert it to one type to other.
help me
Posted 10-Jan-12 21:42pm
Edited 10-Jan-12 21:44pm
Mehdi Gholam240.5K
v2
Comments
vlad781 at 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
good
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));
            bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);
 
            //Set the image of the PictureBox to that image.
            pic.Image = bitmap;
            this.Controls.Add(pic);
            pic.BringToFront();
 
            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);
            g.ReleaseHdc();
            g2.ReleaseHdc();
            g.Dispose();
            g2.Dispose();
 
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
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];
    }
}
  Permalink  
Comments
ranjith m amin at 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
good
Please Sign up or sign in to vote.

Solution 2

The following articles contain useful information about converting ImageSource to BitmapImage and Bitmap:
http://stackoverflow.com/questions/920517/convert-imagesource-to-bitmapimage-wpf
 
http://stackoverflow.com/questions/4506692/how-to-convert-imagesource-to-bitmap
 
There's also a solution that uses a third party toolkit here:
http://support.leadtools.com/CS/forums/41168/ShowPost.aspx
  Permalink  
v2

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



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 11 Jun 2012
Copyright © CodeProject, 1999-2014
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