Better TIF to PNG conversion with Bitmap.Save()? - c#

Update: Tried SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; and it's noticeable at full zoom, but it doesn't solve the problem, the problem is just anti-aliased now...
The TIFs are scanned documents, and contain things like lines for tables and text.
Current approach:
using System.Drawing;
using System.Drawing.Imaging;
var image = Image.FromFile(tifFileName);
Image bitmap = new Bitmap(image, (int)(image.Width), (int)(image.Height));
var imageFinal = new Bitmap(image.Width, image.Height);
var graphic = Graphics.FromImage(imageFinal);
graphic.DrawImage(image, 0, 0, image.Width, image.Height);
using(var imgStream = new MemoryStream())
{
imageFinal.Save(imgStream, ImageFormat.Png);
return new MemoryStream(imgStream.GetBuffer());
}
But, it ends up looking like garbage, for example any kind of slightly slanted line has a hint of a stair step, and other fine elements such as text look rough. Especially in comparison to using GIMP to save a TIF as a PNG, which looks great.
So, is there something I can add to make this work better? Or am I going to have to find another approach entirely?

My immediate impression is that you're going to too much trouble, since you aren't resizing:
var image = Image.FromFile(#"C:\Sample.tiff");
image.Save(#"C:\Sample.png", ImageFormat.Png);
If using the Image type doesn't solve your aliasing problems, try picking your encoder manually:
#region Using Directives
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
#endregion
namespace TiffToBitmap
{
internal class Program
{
private static void Main()
{
// Just save the image.
SaveImage(#"C:\Sample1.png", "image/tiff");
// Get a byte array from the converted image.
var imageBytes = GetBytes("image/tiff");
// Save it for easy comparison.
File.WriteAllBytes(#"C:\Sample2.png", imageBytes);
}
private static byte[] GetBytes(string mimeType)
{
var image = Image.FromFile(#"C:\Sample.tiff");
var encoders = ImageCodecInfo.GetImageEncoders();
var imageCodecInfo = encoders.FirstOrDefault(encoder => encoder.MimeType == mimeType);
if (imageCodecInfo == null)
{
return null;
}
using (var memoryStream = new MemoryStream())
{
var imageEncoderParams = new EncoderParameters(1);
imageEncoderParams.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
image.Save(memoryStream, imageCodecInfo, imageEncoderParams);
return memoryStream.GetBuffer();
}
}
private static void SaveImage(string path, string mimeType)
{
var image = Image.FromFile(#"C:\Sample.tiff");
var encoders = ImageCodecInfo.GetImageEncoders();
var imageCodecInfo = encoders.FirstOrDefault(encoder => encoder.MimeType == mimeType);
if (imageCodecInfo == null)
{
return;
}
var imageEncoderParams = new EncoderParameters(1);
imageEncoderParams.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
image.Save(path, imageCodecInfo, imageEncoderParams);
}
}
}

Here is my realization of image/tiff type conversion:
Convertion block:
/// <summary>
/// Convert Tiff image to another mime-type
/// </summary>
/// <param name="tiffImage">Source Tiff file</param>
/// <param name="mimeType">Desired result mime-type</param>
/// <returns>Converted image</returns>
public Bitmap ConvertTiffToBitmap(Image tiffImage, string mimeType)
{
var imageCodecInfo = ImageCodecInfo.GetImageEncoders().First(encoder => encoder.MimeType == MimeType.Tiff);
using (var memoryStream = new MemoryStream())
{
// Setting encode params
var imageEncoderParams = new EncoderParameters(1)
{
Param = {[0] = new EncoderParameter(Encoder.Quality, 100L)}
};
tiffImage.Save(memoryStream, imageCodecInfo, imageEncoderParams);
var converter = new ImageConverter();
// Reading stream data to new image
var tempTiffImage = (Image)converter.ConvertFrom(memoryStream.GetBuffer());
// Setting new result mime-type
imageCodecInfo = ImageCodecInfo.GetImageEncoders().First(encoder => encoder.MimeType == mimeType);
tempTiffImage?.Save(memoryStream, imageCodecInfo, imageEncoderParams);
return new Bitmap(Image.FromStream(memoryStream, true));
}
}
public static class MimeType
{
public const string Bmp = "image/bmp";
public const string Gif = "image/gif";
public const string Jpeg = "image/jpeg";
public const string Png = "image/png";
public const string Tiff = "image/tiff";
}
Usage:
var sourceImg = Image.FromFile(#"C:\MyImage.tif", true);
var codec = ImageCodecInfo.GetImageDecoders().First(c => c.FormatID == sourceImg.RawFormat.Guid);
if (codec.MimeType == MimeType.Tiff)
{
sourceImg = ConvertTiffToBitmap(sourceImg, MimeType.Jpeg);
}
Hope, it could be usefull

Related

Merge multiple multi-page tiff images to a single tiff C#

In my scenario I have 3 or more multi-page tiff images which I need to merge into a single tiff image.
Below is the the code I have tried. It merges in to a single tiff image but only with first page of all tiff images.
private static void MergeTiff(string[] sourceFiles)
{
string[] sa = sourceFiles;
//get the codec for tiff files
ImageCodecInfo info = null;
foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
if (ice.MimeType == "image/tiff")
info = ice;
//use the save encoder
Encoder enc = Encoder.SaveFlag;
EncoderParameters ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
Bitmap pages = null;
int frame = 0;
foreach (string s in sa)
{
if (frame == 0)
{
MemoryStream ms = new MemoryStream(File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, #"C:\Data_Warehouse\SVNRepository\CD.BNS.W5555.LT45555C.D180306.T113850.Z0101\", s)));
pages = (Bitmap)Image.FromStream(ms);
var appDataPath = #"C:\Data_Warehouse\SVNRepository\Tiffiles\";
var filePath = Path.Combine(appDataPath, Path.GetRandomFileName() + ".tif");
//save the first frame
pages.Save(filePath, info, ep);
}
else
{
//save the intermediate frames
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
try
{
MemoryStream mss = new MemoryStream(File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, #"C:\Data_Warehouse\SVNRepository\CD.BNS.W5555.LT45555C.D180306.T113850.Z0101\", s)));
Bitmap bm = (Bitmap)Image.FromStream(mss);
pages.SaveAdd(bm, ep);
}
catch (Exception e)
{
//LogError(e, s);
}
}
if (frame == sa.Length - 1)
{
//flush and close.
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
frame++;
}
}
I need to join multiple tiff images with all pages from each tiff image. Please advise!
Thanks
EDIT: Updated from below answer
if (frame == 0)
{
MemoryStream ms = new MemoryStream(File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, #"C:\OMTest\Working\", s)));
pages = (Bitmap)Image.FromStream(ms);
var appDataPath = #"C:\Data_Warehouse\SVNRepository\Tiffiles\";
var filePath = Path.Combine(appDataPath, Path.GetRandomFileName() + ".tif");
//save the first frame
pages.Save(filePath, info, ep);
//Save the second frame if any
int frameCount1 = pages.GetFrameCount(FrameDimension.Page);
if (frameCount1 > 1)
{
for (int i = 1; i < frameCount1; i++)
{
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
pages.SelectActiveFrame(FrameDimension.Page, i);
pages.SaveAdd(pages, ep);
}
}
}
else
{
//save the intermediate frames
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
try
{
MemoryStream mss = new MemoryStream(File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, #"C:\OMTest\Working\", s)));
Bitmap bm = (Bitmap)Image.FromStream(mss);
int frameCount = bm.GetFrameCount(FrameDimension.Page);
for (int i = 0; i < frameCount; i++)
{
bm.SelectActiveFrame(FrameDimension.Page, i);
pages.SaveAdd(bm, ep);
}
}
catch (Exception e)
{
//LogError(e, s);
}
}
You need to select the active frame to ensure you are getting all pages on the TIFF. In your code you need to get the count of frames and loop through these.
The code in your else block might look something like this:
MemoryStream mss = new MemoryStream(File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, #"C:\Data_Warehouse\SVNRepository\CD.BNS.W5555.LT45555C.D180306.T113850.Z0101\", s)));
Bitmap bm = (Bitmap)Image.FromStream(mss);
int frameCount = bm.GetFrameCount(FrameDimension.Page);
for(int i=0;i<frameCount;i++){
bm.SelectActiveFrame(FrameDimension.Page, i);
pages.SaveAdd(bm, ep);
}
You may have to tweak it as I haven't tested it.
The given code works great to merge single-page TIFF files into a single multi-page TIFF, however, if there are multi-page TIFF files as sources, it will only merge their first page in the resulting TIFF file: the other ones will be discarded.
Since we couldn't find any working samples that could work around this issue, we ended up coding a small C# helper class, which later became a full-fledged multi-platform console application written in .NET Core 2 and C#. We called the project MergeTIFF and we released the whole source code on GitHub under GNU v3 license, so that everyone else can use it as well; we also released the binaries for Windows and Linux (32-bit and 64-bit).
Here's the relevant excerpt of the C# code:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace MergeTiff.NET
{
/// <summary>
/// A small helper class to handle TIFF files
/// </summary>
public static class TiffHelper
{
/// <summary>
/// Merges multiple TIFF files (including multipage TIFFs) into a single multipage TIFF file.
/// </summary>
public static byte[] MergeTiff(params byte[][] tiffFiles)
{
byte[] tiffMerge = null;
using (var msMerge = new MemoryStream())
{
//get the codec for tiff files
ImageCodecInfo ici = null;
foreach (ImageCodecInfo i in ImageCodecInfo.GetImageEncoders())
if (i.MimeType == "image/tiff")
ici = i;
Encoder enc = Encoder.SaveFlag;
EncoderParameters ep = new EncoderParameters(1);
Bitmap pages = null;
int frame = 0;
foreach (var tiffFile in tiffFiles)
{
using (var imageStream = new MemoryStream(tiffFile))
{
using (Image tiffImage = Image.FromStream(imageStream))
{
foreach (Guid guid in tiffImage.FrameDimensionsList)
{
//create the frame dimension
FrameDimension dimension = new FrameDimension(guid);
//Gets the total number of frames in the .tiff file
int noOfPages = tiffImage.GetFrameCount(dimension);
for (int index = 0; index < noOfPages; index++)
{
FrameDimension currentFrame = new FrameDimension(guid);
tiffImage.SelectActiveFrame(currentFrame, index);
using (MemoryStream tempImg = new MemoryStream())
{
tiffImage.Save(tempImg, ImageFormat.Tiff);
{
if (frame == 0)
{
//save the first frame
pages = (Bitmap)Image.FromStream(tempImg);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
pages.Save(msMerge, ici, ep);
}
else
{
//save the intermediate frames
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
pages.SaveAdd((Bitmap)Image.FromStream(tempImg), ep);
}
}
frame++;
}
}
}
}
}
}
if (frame >0)
{
//flush and close.
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
msMerge.Position = 0;
tiffMerge = msMerge.ToArray();
}
return tiffMerge;
}
}
}
For additional info and/or to download it, you can take a look at the following resources that we published to better document the whole project:
MergeTIFF on GitHub
Specifications, dependencies and other info

Compress Image size and return image c#

I am making a function where I pass an Image and that will compress the image and return that image object. Following is my code:
public Image CompressImage(Image img)
{
ImageCodecInfo jpegCodec = null;
EncoderParameter imageQualitysParameter = new EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, 50L);
ImageCodecInfo[] alleCodecs = ImageCodecInfo.GetImageEncoders();
EncoderParameters codecParameter = new EncoderParameters(1);
codecParameter.Param[0] = imageQualitysParameter;
for (int i = 0; i < alleCodecs.Length; i++)
{
if (alleCodecs[i].MimeType == "image/jpeg")
{
jpegCodec = alleCodecs[i];
break;
}
}
Image img1;
using (Stream memory = new MemoryStream())
{
img.Save(memory, jpegCodec, codecParameter);
img1 = Image.FromStream(memory);
memory.Close();
memory.Dispose();
}
return img1;
}
But When I am saving it in the memory stream, it is showing error
An unhandled exception of type 'System.ArgumentException' occurred in EnexolImageConversionApp.exe
Additional information: Parameter is not valid.
on line
img.Save(memory, jpegCodec, codecParameter);
Try this code - it selects proper ImageCodecInfo by comparing ImageFormat.Guid and ImageCodecInfo.FormatID:
using System.Drawing.Imaging;
using System.IO;
public static byte[] SaveImageToByteArray(Image image, int jpegQuality = 90)
{
using (var ms = new MemoryStream())
{
var jpegEncoder = GetEncoder(ImageFormat.Jpeg);
var encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, (long)jpegQuality);
image.Save(ms, jpegEncoder, encoderParameters);
return ms.ToArray();
}
}
private static ImageCodecInfo GetEncoder(ImageFormat format)
{
var codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
Last step will be loading image again from received byte array.
For better issue understanding please provide exception details, including stacktrace.

C# Image to base64

I have a base64 image and i have to convert it into Image. I am doing this using this code:
public static Image ConvertBase64StringToImage(string imageBase64String)
{
var imageBytes = Convert.FromBase64String(imageBase64String);
var imageStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
imageStream.Write(imageBytes, 0, imageBytes.Length);
var image = Image.FromStream(imageStream, true);
return image;
}
Then i have to convert this Image into base64 again. Im using this code:
public static string ConvertImageToBase64String(Image image)
{
var imageStream = new MemoryStream();
image.Save(imageStream, ImageFormat.Png);
imageStream.Position = 0;
var imageBytes = imageStream.ToArray();
return Convert.ToBase64String(imageBytes);
}
After converting the original base64Image to image and again to base64, it loses quality.The Original base64 image is 1676 in length and after conversion it has 1660 in length.
I need the same image after conversion because i have to compare it. Any ideas how to do it without losing quality?
According to this page although PNG files are lossless, you can specify how much file compression to use when storing. IMO, as long as the Image comes out the same (you can compare them using some sort of MD5 tool), it doesn't matter if the size of the file is different when you save it, as the actual image is still the same as the original.
This is how to convert and compare your images, if the result from CompareImages is zero, it means that they are equal or very similar.
static void Main(string[] args)
{
Image img = new Bitmap("Koala.jpg");
string img64Bit = ConvertImageToBase64String(img);
Image newImg = ConvertBase64StringToImage(img64Bit);
newImg.Save("KoalaCopy.jpg");
Console.WriteLine(CompareImages(img, newImg));
Console.ReadLine();
}
public static string ConvertImageToBase64String(Image image)
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, image.RawFormat);
return Convert.ToBase64String(ms.ToArray());
}
}
public static Image ConvertBase64StringToImage(string image64Bit)
{
byte[] imageBytes = Convert.FromBase64String(image64Bit);
return new Bitmap(new MemoryStream(imageBytes));
}
public static int CompareImages(Image i1, Image i2)
{
string img1 = ConvertImageToBase64String(i1);
string img2 = ConvertImageToBase64String(i2);
return String.Compare(img1, img2);
}

save Image from Image-Control wpf c#

I made a QR-Code Encoder (WPF, c#) by using ZXing.net
I am displaying the QR-Code in an Image-Control
var writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new ZXing.Common.EncodingOptions
{
Height = 200,
Width = 200,
Margin = 0
}
};
var image = writer.Write(qrtext.Text);
qrImg.Source = image;
After that I want to save the image. I was using this example Save Image in a Folder.
private void btnSaveImg_Click(object sender, RoutedEventArgs e)
{
string filePath = #"C:\Users\xxx\Desktop\image.png";
SaveToPng(qrImg, filePath);
}
void SaveToBmp(FrameworkElement visual, string fileName)
{
var encoder = new BmpBitmapEncoder();
SaveUsingEncoder(visual, fileName, encoder);
}
void SaveToPng(FrameworkElement visual, string fileName)
{
var encoder = new PngBitmapEncoder();
SaveUsingEncoder(visual, fileName, encoder);
}
// and so on for other encoders (if you want)
void SaveUsingEncoder(FrameworkElement visual, string fileName, BitmapEncoder encoder)
{
RenderTargetBitmap bitmap = new RenderTargetBitmap((int)visual.ActualWidth, (int)visual.ActualHeight, 96, 96, PixelFormats.Pbgra32);
bitmap.Render(visual);
BitmapFrame frame = BitmapFrame.Create(bitmap);
encoder.Frames.Add(frame);
using (var stream = File.Create(fileName))
{
encoder.Save(stream);
}
}
Unfortunately the image is not being saved. Furthermore, I get no exception. Hope you see my mistake.
Thx a lot
I found another solution, based on this post: How can I save the picture on image control in wpf?
So my solution, that works for me, is:
String filePath = #"C:\Users\xxx\Desktop\test.jpg";
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create((BitmapSource)qrImg.Source));
using (FileStream stream = new FileStream(filePath, FileMode.Create))
encoder.Save(stream);

How to do on the fly image compression in C#?

I have a web application that allows users to upload images of various formats (PNG, JPEG, GIF). Unfortunately my users are not necessarily technical and end up uploading images that are of way too high quality (and size) than is required.
Is there a way for me to compress these images when serving them? By compress I mean lossy compression, reducing the quality and not necessarily the size. Should I be storing the file format as well if different formats are compressed differently?
Of course you can compress images on the fly using C# and .NET.
Here is a function to do so. First it sets up an Encoder object of type jpg and adds one parameter for the new quailty to it. This is then used to write the image with its new quality to a MemoryStream.
Then an image created from that stream is drawn onto itself with the new dimensions..:
//..
using System.Drawing.Imaging;
using System.IO;
//..
private Image compressImage(string fileName, int newWidth, int newHeight,
int newQuality) // set quality to 1-100, eg 50
{
using (Image image = Image.FromFile(fileName))
using (Image memImage= new Bitmap(image, newWidth, newHeight))
{
ImageCodecInfo myImageCodecInfo;
System.Drawing.Imaging.Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
myImageCodecInfo = GetEncoderInfo("image/jpeg");
myEncoder = System.Drawing.Imaging.Encoder.Quality;
myEncoderParameters = new EncoderParameters(1);
myEncoderParameter = new EncoderParameter(myEncoder, newQuality);
myEncoderParameters.Param[0] = myEncoderParameter;
MemoryStream memStream = new MemoryStream();
memImage.Save(memStream, myImageCodecInfo, myEncoderParameters);
Image newImage = Image.FromStream(memStream);
ImageAttributes imageAttributes = new ImageAttributes();
using (Graphics g = Graphics.FromImage(newImage))
{
g.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //**
g.DrawImage(newImage, new Rectangle(Point.Empty, newImage.Size), 0, 0,
newImage.Width, newImage.Height, GraphicsUnit.Pixel, imageAttributes);
}
return newImage;
}
}
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo ici in encoders)
if (ici.MimeType == mimeType) return ici;
return null;
}
Setting the new dimensions is up to you. If you never want to change the dimensions, take out the parameters and set the values to the old dimensions in the code block If you only sometimes want to change you could pass them in as 0 or -1 and do the check inside..
Quality should be around 30-60%, depending on the motifs. Screenshots with text don't scale down well and need around 60-80% to look good and crispy.
This function returns a jpeg version of the file. If you want, you could create a different Encoder, but for scalable quality, jpeg usually is the best choice.
Obviously you could as well pass in an image instead of a filename or save the newImage to disk instead of returning it. (You should dispose of it in that case.)
Also: you could check the memStream.Length to see if the results are too big and adjust the quality..
Edit: Correction //**
Here is a small application written in .net 4.0 that compresses images in live.
Maybe it can help
https://github.com/user5934951/CompressImageLiveNet
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace export
{
class Program
{
static void Main(string[] args)
{
foreach (string file in Directory.EnumerateFiles(#"C:\Temp\Images"))
{
FileInfo file_info = new FileInfo(file);
var filePathOriginal = Path.Combine(#"C:\Temp\Images",
String.Format("{0}", file_info.Name));
if (File.Exists(filePathOriginal) && file_info.Name.Contains(".png"))
{
Task.Factory.StartNew(() => VaryQualityLevel(filePathOriginal));
Console.WriteLine("Compressed {0}. {1}", count, file_info.Name);
count++;
}
}
Console.WriteLine("End Compressing {0} Images{2}{2}Date: {1}{2}", count, DateTime.Now, Environment.NewLine);
Console.WriteLine("Done");
Console.ReadLine();
}
static void VaryQualityLevel(string file)
{
var img = new Bitmap(file);
try
{
SaveJpeg(img, file);
img.Dispose();
if (File.Exists(file))
File.Delete(file);
File.Move(file + 1, file);
}
catch (Exception ex)
{
// Keep going
}
}
static void SaveJpeg(Image img, string filename)
{
EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, 100L);
ImageCodecInfo jpegCodec = GetEncoder(ImageFormat.Jpeg);
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
img.Save(filename + 1, jpegCodec, encoderParams);
}
static ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
}

Categories

Resources