C# Bitmap.Save Method , JPEG - c#

Im creating a new Bitmap object as below,
var image = new Bitmap(#"C:\file.jpg");
I will be doing several modifications in pixel levels in the spatial domain and if save this object again as below.
image.Save(#"D:\final.jpg", ImageFormat.bitmap);
Is this final image is a really a jpg or a bitmap ? ,
Using this functional is there a way we can save a jpg lossless ?

image.Save(#"D:\final.jpg", ImageFormat.Bmp);
will save a bitmap image in spite of the extension
Jpg is a lossy compression method, if you want loseless you can use either Bmp or Png

The Bitmap class is always in a 'raw' format. So what happens in your code is:
Read from a JPEG file (lossy format) into a Bitmap class (raw lossless format).
Save the contents of the Bitmap class into another file (BMP lossless format).
The contents of the Bitmap class after that is still in raw lossless format so that you can continue to manipulate it, or save it to file in yet another format (lossless or lossy).
Note: The 'raw' format I mentioned above is not the same format as some .raw files that you sometimes encounter in music editing programs. By 'raw' format I just mean some temporary arbitrary format that Bitmap uses to store the image data in itself (possibly in byte[]).

Related

How to save a TIFF image format CMYK with Alpha channel in C#

I'm developing an application that loads a TIFF image, concatenate it with another image I created in runtime and save the concatenated one without changing format of the original TIFF.
I'm able to do that using TiffBitmapEncoder & TiffBitmapDecoder + BitmapSource.CopyPixels - With CMYK tiff images only
A special case is TIFF CMYK with an additional Alpha channel (yes, it's 40bits depth), and I'm stuck.
I need to use TiffBitmapDecoder to save the file after modification, but I can only specify format PixelFormats.Cmyk32 for it, and that is not the correct format in case having Alpha channel.
The original format wasn't recognizable by TiffBitmapEncoder fyi..
What can I do/try here to achieve my purpose?

How to get the image format of a BitmapImage?

I have a WPF application with an Image element whose source is bound to a property of type BitmapImage. I need to save this image into our database along with it's format (jpg, tif, basically I'm trying to use the ImageFormat class). How can I get the format of the BitmapImage?
The Image is a control that displays a BitmapImage. There is no meaning to save the Image. You want to save the bitmapImage (the image.Source). Also you should not care of the original encoding/decoding of the bitmapImage. This has to do with the data compression in the file on the disk. Now the thing is in memory as BitmapImage with no compression. You have to decide of the encoding method of your local file. For .jpg you have to run the following code :
BitmapEncoder bme = new JpegBitmapEncoder();
bme.Frames.Add(BitmapFrame.Create(image.Source));
using (var filestream = new System.IO.FileStream("temp.jpg", System.IO.FileMode.Create))
{
bme.Save(fileStream);
}
How can I get the format of the BitmapImage?
You can't, because it does not have a fixed format.
You may use any of the classed derived from BitmapEncoder to encode a BitmapSource. The format is then determined by the BitmapEncoder that is actually used, e.g. PNG when you use PngBitmapEncoder.

File.WriteAllBytes or FileStream.Write

What's the difference between File.WriteAllBytes and FileStream.Write/WriteBytes? I have a bitmap object that and I want to create a new bmp/jpg/png on disk. I think I read somewhere that WriteAllBytes uses FileStream.Write underneath?
WriteAllBytes is just a convinience method, that wraps the underlying Stream operations. (Create a file, write to stream, close stream, etc). Use it if it fits your needs. If you need more control on the underlying operations, fallback to using a Streamor similar.
It is all about using the right abstraction for the task.
Use WriteAllBytes to just save all the bytes, use Write if you need to watch the progress.
You're on the wrong track with this. Saving a bitmap object requires Image.Save(). That's a method that knows how to use an image encoder to convert a bitmap into the bytes that another program (or yours) can load back. There are several image encoders, you can select the one you want with the Save() overload that lets you pick the ImageFormat. The BMP format is the native Windows format, it is uncompressed. The PNG format is nice, it is a compressed lossless format. The JPEG format is a compressed lossy format, good for photos. File size is big to small in order.
You should use WriteAllBytes if you want to save a bitmap.

Can bitmap object be save as PNG or JPEG file format

Is C# bitmap supporting saving the object to JPEG or PNG file format?
Bitmap extends Image, therefore you can call: Image.Save (String, ImageFormat). For example:
using System.Drawing
// ...
Bitmap img = new Bitmap("file.jpg");
img.Save("file.png", ImageFormat.Png); // ImageFormat.Jpeg, etc
Omitting the second argument and just calling Image.Save(String) will save the image as its raw format.
In addition to last post (can't comment existing post since i'm new here)
File type is NOT based off of the extension.
Just try to do
img.Save("result.bmp")
and
Image.Save("result.bmp", ImageFormat.Bmp);
and you'll see that file sizes are dramatically different.

How can I read and write JPEG data on a per-pixel basis?

The title pretty much explains my question. I would like to be able to read and write JPEG data on a per-pixel basis using C#.
I'm thinking something along the lines of CreateJPEG(x, y) which would set up a blank JPEG image in memory, and would give me a JPEG object, and then something like SetPixel(x, y, Color) and GetPixel(x, y) the latter of which would return a Color or something similar. You could then call an Apply() or Save() method, for example, to save the image in a standard JPEG-readable format (preferrably with compression options, but that's not necessary).
And I'm assuming some C# library or namespace makes this all a very easy process, I'd just like to know the best way to go about it.
Have a look at the Bitmap class. For advanced drawing besides manipulating single pixel you will have to use the Graphics class.
var image = new Bitmap("foo.jpg");
var color = image.GetPixel(1, 2);
image.SetPixel(42, 42, Color.White);
image.Save("bar.jpg", ImageFormat.Jpeg);
As Lasse V. Karlsen mentions in his answer this will not really manipulate the JPEG file. The JPEG file will be decompressed, this image data will be altered, and on saving a new JPEG file is created from the altered image data.
This will lower the image quality because even recompressing an unaltered image does usually not yield a bit-identical JPEG file due to the nature of lossy JPEG compressions.
There are some operations that can be performed on JPEG files without decompressing and recompressing it - for example rotating by 90° - put manipulating single pixels does not fit in this category.
JPEG is not a processing format, it's a storage format.
As such, you don't actually use a JPEG image in memory, you just have an image. It's only when you store it that you pick the format, like PNG or JPEG.
As such, I believe you're looking for the Bitmap class in .NET.

Categories

Resources