How to save a .NET Bitmap using FreeImage.NET - c#

After spending 2 days to realize that the C# Bitmap.Save method was bugged (for JPEG/grayscale/8bbp), I tried FreeImage to see if I could save it correctly, and at first glance it seemed so, but after closer inspection it seems it doesn't work either.
Here are my tests:
If I do
FreeImage.SaveBitmap(aImage, aSavePath, FREE_IMAGE_FORMAT.FIF_JPEG, FREE_IMAGE_SAVE_FLAGS.DEFAULT);
the image DPI's aren't saved correctly and if I convert the Bitmap into a FIBITMAP (so that I can specify the DPI's
MemoryStream imageStream = new MemoryStream();
aImage.Save(imageStream, aImageFormat);
FIBITMAP dib = FreeImage.LoadFromStream(imageStream, FREE_IMAGE_LOAD_FLAGS.JPEG_ACCURATE, freeImageFormat);
FreeImage.SetResolutionX(dib, (uint)aImage.HorizontalResolution);
FreeImage.SetResolutionY(dib, (uint)aImage.VerticalResolution);
FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, dib, aSavePath, FREE_IMAGE_SAVE_FLAGS.DEFAULT);
Any ideas on how I can save a Bitmap using FreeImage and preserve the DPI's and bpp's? Or is the FreeImage.Save method also bugged?

Some software relies on metadata while getting resolution, so you can try to add EXIF with DPI fields to jpeg created with FreeImage.
P.S. Your task seems to be pretty easy, I’m sure you can find a few more SDKs for that.

I gave up on this 2 options (FreeImage and Magick.NET) and went with GraphicsMagick.NET.

Related

Cannot save array of png images: "generic error in GDI+" [duplicate]

Having a code that works for ages when loading and storing images, I discovered that I have one single image that breaks this code:
const string i1Path = #"c:\my\i1.jpg";
const string i2Path = #"c:\my\i2.jpg";
var i = Image.FromFile(i1Path);
i.Save(i2Path, ImageFormat.Jpeg);
The exception is:
System.Runtime.InteropServices.ExternalException occurred
A generic error occurred in GDI+.
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at ...
As far as I can see, there is nothing special about the image. It is approx 250 pixels in size and can be opened in e.g. Windows Image Viewer or Paint.NET:
(Since the image above, after being uploaded to Stack Overflow does not produce the error anymore, I've put the original image here)
What I discovered is that upon calling the Save method, the destination image file is being created with zero bytes.
I am really clueless on what causes the error.
My questions:
Can you think of any special thing that would hinder .NET from saving the image?
Is there any way (beside panicing) to narrow down these kind of errors?
While I still did not find out the reason what exactly caused the error when saving the image, I found a workaround to apply:
const string i1Path = #"c:\my\i1.jpg";
const string i2Path = #"c:\my\i2.jpg";
var i = Image.FromFile(i1Path);
var i2 = new Bitmap(i);
i2.Save(i2Path, ImageFormat.Jpeg);
I.e. by copying the image internally into a Bitmap instance and saving this image instead of the original image, the error disappeared.
I'm assuming that by copying it, the erroneous parts the caused the original Save call to fail are being removed an/or normalized, thus enabling the save operation to succeed.
Interestingly, the so stored image has a smaller file on disk (16 kB) than its original source (26 kB).
First of all make sure, that the desired folder has Read/Write permissions. Changing the permissions solved this problem for me.
Solution is here, you must dispose image object to release the memory on the server.
Try use using statement. Make sure destination directory on server exists too.
The reason may be that the image is loaded lazily and the loading process is not yet finished when you try to save it.
Following what's said in this blog post (assuming you're German by the picture you linked in your question) provides a possible solution. Also this SO question's accepted answer indicates this is due to the fact the image file you're trying to save to is locked.
EDIT
For Ulysses Alves, from the linked blog entry: If you load an image using Image.FromFile() it remains locked until it is disposed of. This prevents calls to Save().
pictureBox1.Image = Image.FromFile("C:\\test\\test1.jpg");
pictureBox1.Image.Save("C:\\test\\test2.jpg");
The above code throws an error.
To make it work, you need to copy the image. The following code works:
pictureBox1.Image = Image.FromFile("C:\\test\\test1.jpg");
Image copy = pictureBox1.Image;
copy.Save("C:\\test\\test2.jpg")
I found this question because I also faced the similar error and the file was actually created with zero length (if you don't see any file, first check the permissions to write into folder as other answers suggest). Although my code was slightly different (I use stream to read the image from memory, not from file), I think my answer may be helpful to anyone facing similar problem.
It may looks counter-intuitive, but you can't really dispose memory stream until you finish with image.
NOT WORKING:
Image patternImage;
using (var ms = new MemoryStream(patternBytes)) {
patternImage = new Bitmap(ms);
}
patternImage.Save(patternFile, ImageFormat.Jpeg);
Just don't dispose the stream until you done with image.
WORKS:
using (var ms = new MemoryStream(patternBytes)) {
patternImage = new Bitmap(ms);
patternImage.Save(patternFile, ImageFormat.Jpeg);
}
What is misleading:
Error message doesn't really tell you anything
You can see the image properties, like width and height, but can't
save it
my solution was to make, write temp content (File.WriteAllText) just before saving the file
Here is the code:
var i = Image.FromFile(i1Path);
File.WriteAllText(i2Path, "empty"); // <---- magic goes here
i.Save(i2Path, ImageFormat.Jpeg);
Please try and let me know
In my case I have accidentally deleted the directory where image was getting stored.
Key Information:
// Using System.Drawing.Imaging:
new Bitmap(image).Save(memoryStream, ImageFormat.Jpeg);
You MUST Cast the Image to a Bitmap to Save it.
Using:
// Using System.Drawing.Imaging:
image.Save(memoryStream, ImageFormat.Jpeg);
WILL throw the Error:
Generic GDI+ error when saving an image
Just use the visual studio as administrator or run the application created by the code as administrator it should work smoothly.
It is user access rights issue.
I faced the same and resolved it by running visual studio as administrator.
In my case, I set validateImageData to false:
Image.FromStream(stream, validateImageData: false);
solution:
Image.FromStream(stream, validateImageData: true);
Open in the program
const string i1Path = #"c:\my\i1.jpg";
const string i2Path = #"c:\my\i2.jpg";
var i = Image.FromFile(i1Path);
i.Save(i2Path, ImageFormat.Jpeg);
i.Dispose();

JPEG decoder for .NET Core

there is currently no "built in" JPEG decoder class for .NET Core since System.Drawing (except for System.Drawing.Primitives) is currently not present as a nuget package.
I do understand that System.Drawing relies on underlying Win32 GDI code which obviously is not present on all platforms.
I did read some posts about possible implementations and there appear to be some alpha-grade JPEG packages on nuget but I haven't been able to find a proper one.
Does anyone know about a simple way of DECODING JPEG pictures on .NET Core for some server side processing? I don't even need resize or other functions, decoding would suffice perfectly.
Help is greatly appreciated.
Thanks in advance!
-Simon
Have a look at https://github.com/JimBobSquarePants/ImageSharp
With a usage like in these samples:
Sample 1:
// resizing and filter (grayscale)
using (FileStream stream = File.OpenRead("foo.jpg"))
using (FileStream output = File.OpenWrite("bar.jpg"))
{
Image image = new Image(stream);
image.Resize(image.Width / 2, image.Height / 2)
.Grayscale()
.Save(output);
}
Sample 2: Accessing Pixels
Image image = new Image(400, 400);
using (PixelAccessor<Color, uint> pixels = image.Lock())
{
pixels[200, 200] = Color.White;
}
There is an ImageMagick wrapper for .net which looks like it now supports .Net Core: https://magick.codeplex.com
I have also found the following which works like a charm and is extremely slim: https://www.nuget.org/packages/BitMiracle.LibJpeg.NET/

Resize image on the fly in .net and c#

I'm looking for a way to resize images without saving them on the server. The ways that i have found includes a controller file and such.
Is there a way to get the image from the stream, resize it and add it to the response?
Check out ImageResizer - it's a suite of NuGet packages designed for this exact purpose.
It runs eBay in Denmark, MSN Olympics, and a few other big sites.
Dynamic image processing can be done safely and efficiently, but not in a sane amount of code. It's trickier than it appears.
I wouldn't recommend this but you can do next thing:
using (Image img = Image.FromStream(originalImage))
{
using (Bitmap bitmap = new Bitmap(img, width, height))
{
bitmap.Save(outputStream, ImageFormat.Jpeg);
}
}
Be aware that this could cause OutOfMemoryException.

Displaying EXR files in Windows Forms

I'm struggling with FreeImage and the documentation is not helping me a lot!
I need to display a tif, an exr or an HDR image in a picturebox with C# and I'm not succeeding and I wonder how can I do it... I'm getting the error: Only bitmaps with type of FIT_BITMAP can be converted. ...
Can anyone help me with it ? I suppose I have to convert the tiff to a bitmap but I've tried but I don't know how I should do it yet ... Here is my code:
FIBITMAP imageToDisplay = new FIBITMAP();
imageToDisplay = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_TIFF, i, FREE_IMAGE_LOAD_FLAGS.TIFF_CMYK);
Bitmap bitmap = FreeImage.GetBitmap(imageToDisplay);
pictureBox.Image = (Image)new Bitmap(bitmap);
For displaying TIFFs, PictureBox.Image takes a System.Drawing.Image object - and System.Drawing.Image.FromFile() supports TIFF images.
I can't see any need to involve any third party dependencies here. It's all built in to the framework.
pictureBox.Image = Image.FromFile(someImage);
If your TIFF isn't a file (e.g. if it's just a byte array or a MemoryStream) - that's okay too - use Image.FromStream().
For unsupported file formats, your job is to convert them into a format supported by System.Drawing.Image. If this is not possible, you might not be able to use the PictureBox control for this job.
Perhaps the title of your question should be "Constructing Image objects from EXR files" or maybe "Displaying EXR files in Windows Forms" or similar.

Loading an image from a stream without keeping the stream open

Is it possible to use the FromStream method of System.Drawing.Image without having to keep the stream open for the lifetime of the image?
I have an application which loads a bunch of toolbar graphics from resource files, using a combination of Image.FromStream and Assembly.GetManifestResourceStream.
The problem I'm having is while this works fine on Windows 7, on Windows XP the application crashes if a user interface element linked to one of these images is disabled. On Windows 7, the image is rendered in grayscale. On XP, it crashes with an out of memory exception.
After a load of hairpulling I have finally traced it to the initial loading of the image. As a matter of course, if I create any object implementing IDisposable that is also destroyed in the same method, I wrap it in a using statement, for example
using (Stream resourceStream = assembly.GetManifestResourceStream(resourceName))
{
image = Image.FromStream(resourceStream);
}
If I remove the using statement so that the stream isn't disposed, then the application no longer crashes on XP. But I now have a bunch of "orphan" streams hanging about - the images are stored in command classes and these correctly dispose of the images when they themselves are disposed, but the original stream isn't.
I checked the documentation for FromStream and it confirms the stream needs to remain open. Why this hasn't crashed and burned on the Windows 7 development system is a mystery however!
I really don't want this stream hanging around, and I certainly don't want to have to store a reference to this stream as well as the image so I can dispose of it later. I only have need of that stream once so I want to get rid of it :)
Is it possible to create the image and then kill of the stream there and then?
The reason the stream needs to be open is the following:
GDI+, and therefore the System.Drawing namespace, may defer the decoding of raw image bits until the bits are required by the image. Additionally, even after the image has been decoded, GDI+ may determine that it is more efficient to discard the memory for a large Bitmap and to re-decode later. Therefore, GDI+ must have access to the source bits for the image for the life of the Bitmap or the Image object.
The documented workaround is to create either a non-indexed image using Graphics.DrawImage or to create an indexed Bitmap from the original image as described here:
Bitmap and Image constructor dependencies
According to the documentation of Image.FromStream, the stream must be kept open while the image is in use. Therefore, even if closing worked (and there's nothing to say you can't close a stream before it's disposed, as far as the stream object itself goes) it may not be a very reliable approach.
You could copy the image to another image object, and use that. However, this is likely to be more memory intensive than just keeping the stream open.
You could save the stream to a temporary file and use the Image.FromFile method. Or simply don't embed the image, keep it as a file and load it from this file at runtime.
I'am sure this will help someone :)
I used it for my dataGridView_SelectionChanged:
private void dataGridViewAnzeige_SelectionChanged(object sender, EventArgs e)
{
var imageAsByteArray = File.ReadAllBytes(path);
pictureBox1.Image = byteArrayToImage(imageAsByteArray);
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}

Categories