JPEG decoder for .NET Core - c#

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/

Related

Resize Image (jpg) in Blazor WebAssembly?

What is the best way to resize an image that is uploaded on a Blazor client page. These are real simple images that I just wanted to have a consistent width and was hoping to use the System.Drawing, but that is not available in web assembly. I was hoping to do it on the client, but is it best to send it to a Controller for processing?
Thanks,
Mike
.NET 5 has an extension method to IBrowserFile called RequestImageFileAsync. Per the documentation, it "Attempts to convert the current image file to a new one of the specified file type and maximum file dimensions."
We use a package PhotoSauce.MagicScaler for all images uploaded from any blazor page.
https://photosauce.net/
I have no affiliation with this package / author etc. Just recommend it because it works so well.
There are lots of params that you can set in the ProcessImageSettings for width, method of resizing etc.
var settings = new ProcessImageSettings { Height = 250 };
using var outStream = new FileStream(thumbNailPathAndFile, FileMode.Create);
MagicImageProcessor.ProcessImage(pathAndFile, outStream, settings);

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.

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.

C# import of Adobe Illustrator (.AI) files render to Bitmap?

Anyone knows how to load a .AI file (Adobe Illustrator) and then rasterize/render the vectors into a Bitmap so I could generate eg. a JPG or PNG from it?
I would like to produce thumbnails + render the big version with transparent background in PNG if possible.
Ofcause its "possible" if you know the specs of the .AI, but has anyone any knowledge or code to share for a start? or perhaps just a link to some components?
C# .NET please :o)
Code is most interesting as I know nothing about reading vector points and drawing splines.
Well, if Gregory is right that ai files are pdf-compatible, and you are okay with using GPL code, there is a project called GhostscriptSharp on github that is a .NET interface to the Ghostscript engine that can render PDF.
With the newer AI versions, you should be able to convert from PDF to image. There are plenty of libraries that do this that are cheap, so I would choose buy over build on this one. If you need to convert the older AI files, all bets are off. I am not sure what format they were in.
private void btnGetAIThumb_Click(object sender, EventArgs e)
{
Illustrator.Application app = new Illustrator.Application();
Illustrator.Document doc = app.Open(#"F:/AI_Prog/2009Calendar.ai", Illustrator.AiDocumentColorSpace.aiDocumentRGBColor, null);
doc.Export(#"F:/AI_Prog/2009Calendar.png",Illustrator.AiExportType.aiPNG24, null);
doc.Close(Illustrator.AiSaveOptions.aiDoNotSaveChanges);
doc = null; //
}
Illustrator.AiExportType.aiPNG24 can be set as JPEG,GIF,Flash,SVG and Photoshop format.
I Have Tested that with Pdf2Png and it worked fine with both .PDF and .ai files.
But I don't know how it will work with transparents.
string pdf_filename = #"C:\test.ai";
//string pdf_filename = #"C:\test.pdf";
string png_filename = "converted.png";
List<string> errors = cs_pdf_to_image.Pdf2Image.Convert(pdf_filename, png_filename);

Reading PSD file format

I wonder if this is even possible. I have an application that adds a context menu when you right click a file. It all works fine but here is what I'd like to do:
If the file is a PSD then I want the program to extract the image. Is this possible to do without having Photoshop installed?
Basically I want the user to right click and click "image" which would save a .jpg of the file for them.
edit: will be using c#
Thanks
The ImageMagick libraries (which provide bindings for C#) also support the PSD format. They might be easier to get started with than getting into the Paint.NET code and also come with a quite free (BSD-like) license.
A simple sample (found at http://midimick.com/magicknet/magickDoc.html) using MagickNet would look like this:
using System;
static void Main(string[] args)
{
MagickNet.Magick.Init();
MagicNet.Image img = new MagicNet.Image("file.psd");
img.Resize(System.Drawing.Size(100,100));
img.Write("newFile.png");
MagickNet.Magick.Term();
}
Note: MagickNet has moved to http://www.codeproject.com/KB/dotnet/ImageMagick_in_VBNET.aspx
Well, there's a PSD plugin for Paint.NET which I think is Open-Source which you might want to take a look at for starters:
http://frankblumenberg.de/doku/doku.php?id=paintnet:psdplugin#download
This guy do it easier:
http://www.codeproject.com/KB/graphics/simplepsd.aspx
With a C# library and a sample project.
I've tried with PS2 files and works ok.
I have written a PSD parser which extracts raster format layers from all versions of PSD and PSB. http://www.telegraphics.com.au/svn/psdparse/trunk
You can use GroupDocs.Viewer for .NET API to render your PSD files as images (JPG, PNG, BMP) in your application using a few lines of code.
C#
ViewerConfig config = new ViewerConfig();
config.StoragePath = "D:\\storage\\";
// Create handler
ViewerImageHandler imageHandler = new ViewerImageHandler(config);
// Guid implies that unique document name
string guid = "sample.psd";
// Get document pages as images
List<PageImage> pages = imageHandler.GetPages(guid);
foreach (PageImage page in pages)
{
// Access each image using page.Stream
}
For more details and sample code, please visit here.
Disclosure: I work as a Developer Evangelist at GroupDocs.
For people who are reading this now: the link from accepted answer doesn't seem to work anymore (at least for me). Would add a comment there, but not allowed to comment yet - hence I'm adding a new answer.
The working link where you can find the psdplugin code for Paint.Net: https://github.com/PsdPlugin/PsdPlugin
Here is my own psd parser and exporter:
http://papirosnik.info/psdsplit/.
It allows to correctly parse psd with rgb color 8, 16- and 32-bit for channel, process user masks, export selected layers into jpeg, png, jng, bmp, tiff; create xml layout of exported layers and groups and also create a texture atlas and animations set from given layers.
It's entirely written in C#. If you want its sources inform me via support link on About dialog in the application.
ImageMagick.NET - http://imagemagick.codeplex.com/ - is the later version of the link 0xA3 gave, with some slightly different syntax. (Note, this is untested):
using ImageMagickNET;
public void Test() {
MagickNet.InitializeMagick();
ImageMagickNET.Image img = new ImageMagickNET.Image("file.psd");
img.Resize(new Geometry(100, 100, 0, 0, false, false);
img.Write("newFile.png");
}
I got extraction from psd working. see my answer here
How to extract layers from a Photoshop file? C#
may help someone else.
FastStone does this pretty efficiently.
They do not have their libraries availaible, but I guess you can contact them and see if they can help.
Check out their website: http://www.faststone.org/download.htm
I've had great success with Aspose's Imaging component which can load and save PSD files without Photoshop: https://products.aspose.com/imaging/net

Categories