Difference between WriteableBitmap and Bitmap in C# - c#

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

Related

WritableBitmapImage from BitmapImage with no source Windows Phone 8.1 RT

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.

What is a way to combine different images to form a single image?

I want to make a cartoon using face.jpg hair.jpg eyes.jpg and all other .jpg files of other facial features to make a single image of a cartoon. I know that i can draw lines,circles,ellipses etc. on image bitmap using its graphics object but is there a way to draw a image bitmap on a Graphics object?. Any Help would be appreciated.
If you have the Image instance, you can use Graphics.DrawImage method.

Changing pixels in Image

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/

Image vs BitmapImage vs Bitmap

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...

Resizing Resulting Camera Stream

I am trying to let a user capture an image and add it onto the screen using an Image. However, I also need to resize this image down to about half size due to memory restrictions (12x 5MP images is never good on a phone...)
I am launching the camera task fine and it calls the Completed event. However, when I try and use DecodeJpeg I get a "The parameter is incorrect." exception.
Here is my code for resizing, where mx and my are int for dimensions. I have verified that there is something in the e.ChosenPhoto with a length of about ~5500:
WriteableBitmap bitmap = PictureDecoder.DecodeJpeg(e.ChosenPhoto, mx, my);
Image img = new Image();
img.Source = bitmap;
The first line is where the app crashes. Any ideas?
EDIT:
This also occurs with the result from the PhotoChooserTask....
Try using the System.Windows.Media.Imaging - Extensions.LoadJpeg method instead of PictureDecoder.DecodeJpeg. Also make sure that the stream is positioned at the beginning of the stream. If you have already used the stream you will need to reset it using:
MyImageStream.Seek(0, System.IO.SeekOrigin.Begin)
I had a lot of problems trying to get access to the original image, especially since BitmapImage automatically resizes images over 2000x2000. If you want an image larger than 2000x2000 you have to have access to the original stream and load it into a WriteableBitmap object
If you want to see some more complex image handling code including detecting resolution from image stream using ExifLib and rotating stream using the WriteableBitmap Extensions check out the BarcodeCaptureResult class for the Silverlight ZXing Library.
UPDATE: Since all you want is to resize an image given the e.ChosenPhoto result I pulled the code from The Silverlight ZXing library. This should work:
WriteableBitmap wbBarcodeImage = new WriteableBitmap(mx, my);
Extensions.LoadJpeg(wbBarcodeImage, e.ChosenPhoto);//Load JPEG from stream into our re-sized writeable bitmap
Note that you will need to use the correct height/width ratio, otherwise you will have a black bar at the bottom or side of the image. You can use ExifLib to detect the original image size and use that to scale (see GetWriteableBitmap method in BarcodeCaptureResult linked above)

Categories

Resources