Click here to Skip to main content
12,945,575 members (51,849 online)
Rate this:
 
Please Sign up or sign in to vote.
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
System.Windows.Media.Imaging.BitmapImage.

How to convert it to one type to other.
help me
Posted 10-Jan-12 21:42pm
Updated 10-Jan-12 21:44pm
Mehdi Gholam335.9K
v2
Comments
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
 
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 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
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  

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
OriginalGriff 4,899
CHill60 3,170
Maciej Los 2,443
Jochen Arndt 1,910
ppolymorphe 1,775


Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 11 Jun 2012
Copyright © CodeProject, 1999-2017
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