Displaying EXR files in Windows Forms - c#

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.

Related

Why won't some Tiff files load with TiffLib.NET

I am having trouble loading Tiff files in C#. I downloaded some sample tiff files and was able to load them fine, however when I attempt to load any tiff files generated from PCI Geomatica or ArcGIS, the ReadRGBAImage call fails (returns false). Other than IMAGEWIDTH and IMAGELENGTH, all of the other tags I've tried to retrieve have returned null (eg. XRESOLUTION). Does anyone have any ideas as to why this is happening? The relevant code is below:
using (Tiff tif = Tiff.Open(fileName, "r"))
{
// Find the width and height of the image
FieldValue[] value = tif.GetField(TiffTag.IMAGEWIDTH);
int width = value[0].ToInt();
value = tif.GetField(TiffTag.IMAGELENGTH);
int height = value[0].ToInt();
// Read the image into the memory buffer
int[] raster = new int[height * width];
if (!tif.ReadRGBAImage(width, height, raster))
{
System.Windows.Forms.MessageBox.Show("Could not read image");
return null;
}
}
Thanks!
Without a file to reproduce the issue, I can be 100% sure, but it looks like your file can not be converted to RGBA raster with help of LibTiff.Net.
It's not an error, it's just you are using some not very popular flavor of TIFF. Some say TIFF is a Thousand of Incompatible File Formats. And there is certainly some truth in that statement.
The library can read (and decode!) your file. You can get decoded raster using ReadEncodedStrip and / or ReadScanline methods. The task of converting your raster to RGBA is left to you.
And don't forget that not every image can be converted to RGBA without loosing some of the image data.

How to save a .NET Bitmap using FreeImage.NET

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.

Document to .jpg converter

We're looking for an all around document converter to create small thumbnails for previewing in our asp.net project. So far we've encountered a handfull of difficult to use/document commandline tools that can convert one filetype to jpg, but we're looking for an all around solution. ( xls, doc, pdf to jpg ). Does anybody know any solutions for this problem with a pricetag that doesn't exceed 700- 1500 $?
Its just an Idea About Creating Thumbnails.
You will have to use image.GetThumbnailImage() method.
It works as follows:
Image img = Image.FromFile(fName);
Image thumb = img.GetThumbnailImage(120, 120, ()=>false, IntPtr.Zero);
thumb.Save(Path.ChangeExtension(fName, "thumb"));
MSDN:
http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage.aspx
Hope Its Helpful.

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.

Load DICOM image using GDCM library in C# and Convert to System.Windows.Control.Image

I want to load a DICOM image using GDCM library in C#.
I have already downloaded/installed the GDCM library but I don't know how to read DICOM image using GDCM and convert it into a format which can be displayed in WPF application.
Could someone please share any piece of code showing me how to achieve this ?
I wasn't playing with dicom, but out of curiosity I've googled sample on how to convert gdcm image to Qt Image (it's in c++, but I hope that c# port offer same functionality)
You can probably just do what they are doing, but interesting part is how to create WPF Image instead of QTImage. Basically, when dealing with native buffers, WriteableBitmap is the class you want to work with. So instead of:
imageQt = new QImage(ubuffer, dimX, dimY, QImage::Format_RGB888);
You may use something like this:
int dimX;
int dimY;
byte* uBuffer; // Those fields are filled from code from this sample
WriteableBitmap bmp = new WriteableBitmap(dimX, dimY, 96.0, 96.0, PixelFormats.Bgr24, null);
bmp.Lock();
bmp.CopyPixels(new Int32Rect(0, 0, dimX, dimY), uBuffer, uBuffer.Length,
uBuffer.Length / dimY);
bmp.Unlock();
WriteableBitmap is BitmapSource, so it can be used just like any other Image in WPF.

Categories