I am trying to work with TIFF images using C# in an ASP.NET environment. The catch, the images may have transparency, and any time I try to work with a transparent TIFF file I get either an Out of Memory Exception or Parameter is not valid error.
Here are the different ways I've gone about it:
string imagename = "Test.tif";
var image = Image.FromFile(#"C:\Path-to-File\" + imagename);
OR
Bitmap myBitmap = new Bitmap(#"C:\Path-to-File\" + imagename);
I've tried throwing it into a filestream and still receive errors. The TIFF files are coming from Photoshop, and I've definitely narrowed down transparency as the culprit.
This link does mention that the Image class does not support transparency
Looking for any sort of guidance...this shouldn't be as difficult as I'm finding.
Thanks to #Andrew Morton suggesting FreeImage, it ended up leading me to the Magick.NET library.
Ultimately FreeImage was near impossible to get to work in a C# program using current versions of Visual Studio, thus from some digging I found Magick.NET which after a quick search in NuGet, was very easy to install and get going.
Here is what I did:
Search for Magick in NuGet and select the desired architecture (x64 or x86)
Note: If selecting x64, the build settings for your project must be set to this same architecture otherwise you will get a failure such as:
Could not load file or assembly 'Magick.NET.DLL' or one of its dependencies. The specified module could not be found.
Familiarize yourself with the basics in this documentation
That's it! Here is the code I used to get it working:
using (MagickImage image = new MagickImage(#"C:\FolderPathToFile\OriginalFile.tif"))
{
image.Write(#"C:\FolderPathToFile\FinalOutput.png");
}
Related
this is my first question in stack overflow (Ye hey)!
So I am using Itext 7 to create PDF files in my program. When creating a PDF, the document inside has an image logo. It work when I run the program in debug mode using this path name:
string imageFile = "../../Resources/smIcon(Transparent).png";
ImageData imagedata = ImageDataFactory.Create(imageFile);
Image image = new Image(imagedata).SetHeight(100).SetWidth(200);
document.Add(image);
However, publishing the project and running the application does not let me create the PDF due to the Path name... I tried publishing without the logo by removing the code above and it works to create a PDF document, which I suspect the issue is the path name.
Would be great to know if you guys have a solution for this! Thank you.
Found a way to work on it with a help of a good friend.
ImageData imagedata = ImageDataFactory.Create(Properties.Resources.smIcon_Transparent_, null);
Image image = new Image(imagedata).SetHeight(100).SetWidth(200);
So basically with this way, I don't need to get the actual path name of the file but instead get the one that is in the properties.resource, where publishing the project does not need to change the path.
In WPF I'm using uri's as ImageSource for items in a list and it works fine except for url's thats is a .svg url.
I've tried using Svg. It works but the images are messed up and I had to download the file locally.
I also tried using SharpVectors didn't understand how to get it to work and got nowhere using the documentation.
This is my original code and it works for png, gif and jpg:
return new BitmapImage(new Uri(url, UriKind.Absolute));
If would also like to have a solution that does not require me to download files locally first but using Stream or the uri directly.
I can recommend CefSharp.Wpf for displaying SVG's images. It is chromium engine wrapped as WPF or Windows control.
I'm using it successfully to even play quite complex SVG animations.
This would give you possibility to show online image without downloading it.
For more details and samples please refer:
GitHub CefSharp
It is available as NuGet package. The only drawback might be memory footprint and necessity of maintaining browser engine process but beside this it is perfect.
I am currently using the evil-dicom library for opening DICOM files in c#. I am able to access the dicom file with:
var dcm = DICOMObject.Read('dicomfilename');
Then open up the pixel stream using
dcm.PixelStream
How can I save the pixelstream as a bmp image?
I never used the toolkit; but at least till Oct-2014, author of the toolkit was saying following:
Evil DICOM does not have any image classes to help with this. While that might seem strange to not include image tools in a DICOM library, it is not the original intent of the the library. Evil DICOM is more for manipulating and analysis of DICOM data. I used to have some image parts in the old library which I believe is still available on SourceForge. You can take a look, but the PixelData tag has the raw bits to put together an image. .NET has several classes that can help with that, but I don't have anything to write here in this post. If I get some time, I will write a blog post about how to do it on the website (rexcardan.com).
Source: GitHub
Apparently, it was not original intent of the toolkit to include imaging support in toolkit. Not sure if this is changed since then. Author was planning to write an article to achieve this through DotNet; not sure if he wrote any then after. Old library from SourceForge may create other issues as it might not have been updated since long.
You can find some example code here.
I need a c# script for unity that can read the EXIF lat/long data from a photo. i would like to place a posTransform at that location. not sure if this is possible within Unity. i would like to load my images into unity and have a script read the EXIF: 1-GPS lat/long, 2-rotation, 3-timestamp from photographs. i haven't found any info that says this can be done within unity, however, i've read about exiflib github project and other ways outside of unity.
THANKS in advance for help
I maintain a project for extracting metadata from images that will give you what you need.
https://github.com/drewnoakes/metadata-extractor-dotnet
The library supports .NET 3.5 so should work under Unity, though I haven't tested it before.
With it, you would write:
var directories = ImageMetadataReader.ReadMetadata(filePath);
var gpsDirectory = directories.OfType<GpsDirectory>().FirstOrDefault();
if (gpsDirectory != null)
{
var location = gpsDirectory.GetGeoLocation();
Console.WriteLine($"Photo was taken at {location.Latitude},{location.Longitude}");
}
The answer from Drew Noakes doesn't work because it has dependencies on .NET that Unity doesn't support.
In my opinion, the best option right now, after a few days of research, is this project:
https://www.codeproject.com/Articles/5251929/CompactExifLib-Access-to-EXIF-Tags-in-JPEG-TIFF-an
This is a simple one-file project that you can drag in your unity project and that has no external dependencies. And it can read and update EXIF tags for JPG, PNG and TIFF files.
I have a component for an image file upload.
However, I want to also be able to check DPI settings because these images will eventually be printed and submitted on paper.
Within ASP.net, I can usually do something like this:
using (var rawBitmap = new Bitmap(postedFile.InputStream)){
var dpi = (decimal)rawBitmap.VerticalResolution/bitmap.Height;
// do other stuff.
}
However, within silverlight, I don't have access to the same libraries in order to do this (that said, this is my first stab at Silverlight, so if there is a way to get those dlls in, I'm all for it, but I couldn't get my utility wrapper imported).
I've seen lots of recommendations for FJcore (imagetools also wraps this library), a JPEG encoding/decoding utility. In theory, one loads up the JPEG stream into the decoder and gets information out.
I've tried using the approach with FJcore, but all the files that I'm saving out of photoshop seem to be missing the correct header that indicates the star of the file, which causes the decoder to fail. I've also confirmed this issue using their unit tests.
Any ideas on how to pull image resolution out of a file upload in silverlight?
DPI of the image is not always stored on the image. This is usually an extra property saved as a metadata during capture by the scanner (or the camera). You can actually see that if you load a JPEG in C# using Bitmap and save it again, DPI property is lost and set to the default 96.
So this is unfortunately not an option always reliable. I do not think there is any chance of getting it for all images. DPI in fact is irrelevant for pictures that are not created by scanners.
Try F J core assemblies to find DPI of image in silverlight
FileStream stream = imageFiles.OpenRead();
DecodedJpeg jpegImage = new JpegDecoder(stream).Decode();
int imageDpi = jpegImage.Image.DensityX;