I was wondering what the difference is between Image, Bitmap and BitmapImage in WPF & C#.
Can someone help me out?
Image is a base abstract class representing images in GDI+. Bitmap is a concrete implementation of this base class.
BitmapImage is a way to represent an image in a vector based GUI engine like WPF and Silverlight. Contrary to a Bitmap, it is not based on GDI+. It is based on the Windows Imaging Component.
There are ways to load a BitmapImage from a Bitmap.
WinForms/GDI+ uses the abstract class System.Drawing.Image and its implementation Bitmap.
WPF uses the abstract class System.Windows.Media.ImageSource (and BitmapSource) and its implementation BitmapImage.
WPF also has a control named Image, which is a FrameworkElement that contains and displays an ImageSource.
It took me a while to untangle that mess of terminology...
Related
I have an BitmapImage which is generated by application itself and have no sourceFile or URI. Now i have to convert this image to WritableBitmapImage in order to save it, according to this. But all the methods require sourceFile or URI.
There is no good way to extract the pixels from a BitmapImage after the fact.
As you note, if you have the source image then you can create a WriteableBitmap from that.
Since WritableBitmap and BitmapImage are both ImageSources they can be used the same way in most cases, so if you know you'll need access to the pixels when you create the BitmapImage then you can usually create a WriteableBitmap instead.
Once the BitmapImage is created and the original source is no longer available the closest you can get is to use RenderTargetBitmap to render the displayed Image control into a new bitmap from which you can extract the pixels with GetPixelData.
This will be a second generation image though and for large images will likely have lost data to resizing interpolation between the original and the rendering.
I'd recommend using a WriteableBitmap instead of a BitmapImage to begin with when generating the original image.
How can I load 2 Images in one PictureBox ?
Here is an example:
http://postimg.org/image/l78kth897/
Thank you so much.
You can use Graphics.DrawImage() to draw any image anywhere inside a PictureBox or any other control for that matter. If you're writing your own control, override OnPaint(). If you want to use an existing PictureBox, simply use its Paint event to do this:
e.Graphics.DrawImage(YourImageObjectHere, ...);
e.Graphics.DrawImage(YourSecondImageObjectHere, ...);
GDI+ already supports transparency channel, so if your images have transparent areas, they'll draw just like the sample image you have posted. DrawImage() has over a dozen overloads, using which you can control several aspects of how an image is drawn. The simplest one takes the image object and the position to draw at.
Remember that an image object is an object of System.Drawing.Image or one of its derived classes. If all you have is the path of the image, you should use Image.FromFile() to create an Image object from that image file first.
I'm having a DICOM image data, which holds 16bit gray values. To visualize the data I need to apply a windowing method that cuts out pieces of the 16bits values. Therefore I have two properties, like WindowCenter and WindowWidth.
Since I want to use the WPF mechanisms I decided to create a DicomImage class which derives from BitmapSource. Since BitmapSource has no direct access to the internal pixeldata, I created a private WriteableBitmap (InternalImage) member where I can manipulate the data whenever WindowCenter and WindowWidth is changed.
To see the results I attach to Image.Source = DicomImage and the image is shown. But when I change the WindowCenter/WindowWidth the image is not updated. When I attach the WriteableBitmap directly (Image.Source = InternalImage) everything works as it should.
What I'm I doing wrong?
Thanks
Marti
I have program in which i want to do some graphic algorithms. I found some code snippets for C# that works on bitmaps. To change pixel or something like that do i have to convert image to bitmap first or is there some methods to change pixels in Image? Something like image1.SetPixel(29,201, color1); ?
You can use the WriteableBitmap class to create a bitmap whose pixels you can modify.
You can then set that Bitmap as the Source for an Image element.
http://books.google.co.uk/books?id=nYl7J7z3KssC&pg=PA416&lpg=PA416&dq=wpf+writeablebitmap&source=bl&ots=V533ojV65x&sig=KJeSje1WCXaS_MT78cR4PPZMFio&hl=en#v=onepage&q=wpf%20writeablebitmap&f=false
http://www.i-programmer.info/programming/wpf-workings/527-writeablebitmap.html?start=1
http://www.nerdparadise.com/tech/csharp/wpfimageediting/
There's a 3rd party library which can help working with WriteableBitmaps more natural i.e. SetPixel and GetPixel methods instead of having to calculate offsets to the pixel data in the buffer.
http://writeablebitmapex.codeplex.com/
What's the difference between the a Bitmap and a WriteableBitmap? I want to display a video stream on a C# form, which would be more suitable in terms of efficiency? isn't a normal bitmap also editable using unsafe code?
WriteableBitmap inherits from System.Windows.Media.ImageSource
Bitmap inherits from System.Drawing.Image
Looks like Bitmap was pre wpf/silverlight where Writeablebitmap is after
You can find a good answer there about what is the real difference between Bitmap and WriteableBitmap:
Bitmap and WriteableBitmap
And about streaming video in C# :
Stream Video C# Solution