Here is the process my user wants.
A customer uploads an image. I must make the image 2.5" x 1.38" due to later requirements.
This may require me to shrink it, enlarge it, or put a white border around it to make it fit properly.
I must handle .PDF, .TIFF, .JPEG, .GIF, etc.
I must allow the user to then crop it down.
I can't seem to get Magick.NET to work. It either turns the image to such high grain that it's not legible, or it turns the image into such a large image that processing is dreadfully slow.
I am currently testing with a 96dpi image, but I am bumping it to 300 dpi to try to avoid the grain problem.
Here is my upload code.
A customer uploads an image. I must make the image 2.5" x 1.38" due to later requirements.
This may require me to shrink it, enlarge it, or put a white border around it to make it fit properly.
I must handle .PDF, .TIFF, .JPEG, .GIF, etc.
I must allow the user to then crop it down.
I can't seem to get Magick.NET to work. It either turns the image to such high grain that it's not legible, or it turns the image into such a large image that processing is dreadfully slow.
I am currently testing with a 96dpi image, but I am bumping it to 300 dpi to try to avoid the grain problem.
Here is my upload code.
#region Convert PDF to TIFF
if (System.IO.Path.GetExtension(imageFile.imagename).ToLower() == ".pdf")
{
using (MagickImage reformat = new MagickImage(Folder + imageFile.imagename))
{
reformat.CompressionMethod = CompressionMethod.LZW;
imageFile.imagename = System.IO.Path.GetFileNameWithoutExtension(imageFile.imagename) + ".tiff";
reformat.Write(Folder + imageFile.imagename);
}
}
#endregion
#region Resample at 300 dpi
using (MagickImage img = new MagickImage(Folder + imageFile.imagename))
{
Boolean changed = false;
if (img.Quality != 100 || img.Depth != 24)
{
img.Quality = 100;
img.Depth = 24;
img.Resample(new PointD(300, 300));
changed = true;
}
if (img.ColorSpace != ColorSpace.sRGB && img.ColorSpace != ColorSpace.RGB)
{
img.AddProfile(ColorProfile.SRGB);
img.ColorSpace = ColorSpace.sRGB;
img.RePage();
changed = true;
}
if (changed)
{
imageFile.imagename = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(imageFile.imagename);
img.Write(Folder + imageFile.imagename);
}
}
#endregion
#region Get Image close to requirements
Single MaxWidth = 0;
Single MaxHeight = 0;
using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Folder + imageFile.imagename))
{
imageFile.HR = bmp.HorizontalResolution;
imageFile.VR = bmp.VerticalResolution;
MaxWidth = ImageWidth * imageFile.HR;
MaxHeight = ImageHeight * imageFile.VR;
Single width = bmp.Width;
Single height = bmp.Height;
if (width != MaxWidth)
{
Single ratio = width / MaxWidth;
height = height * ratio;
width = MaxWidth;
}
if (height > MaxHeight)
{
Single ratio = height / MaxHeight;
width = width * ratio;
height = MaxHeight;
}
}
Single thumbWidth = ThumbWidth;
Single thumbHeight = ThumbHeight;
using (MagickImage img = new MagickImage(Folder + imageFile.imagename))
{
if (img.Quality != 100 || img.Depth != 24)
{
img.Quality = 100;
img.Depth = 24;
img.Resample(new PointD(300, 300));
}
img.Resize(Convert.ToInt32(Math.Floor(MaxWidth)), Convert.ToInt32(Math.Floor(MaxHeight)));
img.BackgroundColor = System.Drawing.Color.Transparent;
if (System.IO.Path.GetExtension(imageFile.imagename).ToLower() == ".tiff"
|| System.IO.Path.GetExtension(imageFile.imagename).ToLower() == ".tif")
{
img.RemoveProfile("tiff:37724");
}
img.Extent(Convert.ToInt32(Math.Floor(MaxWidth)), Convert.ToInt32(Math.Floor(MaxHeight)), Gravity.Center, new MagickColor(System.Drawing.Color.White));
imageFile.imagename = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(imageFile.imagename);
img.Write(Folder + imageFile.imagename);
}
#endregion
Here is my crop code. I am using a javascript cropping tool, and the new image dimensions are passed to this routine. The cropping works, it's the grain of the photo that is unacceptable. using (MagickImage img = new MagickImage(folder + thisModel.originalURL))
{
if (img.Quality != 100 || img.Depth != 24)
{
img.Quality = 100;
img.Depth = 24;
img.Resample(new PointD(300, 300));
}
img.Crop(new MagickGeometry(Convert.ToInt32(x), Convert.ToInt32(y), Convert.ToInt32(w), Convert.ToInt32(h)));
#region Now we upsize it
Double resX = img.ResolutionX;
Double resY = img.ResolutionY;
if (resX == 0 || resY == 0)
{
resX = img.Density.X;
resY = img.Density.Y;
}
img.Resize(Convert.ToInt32(Math.Floor(widthNeeded * Convert.ToSingle(resX))), Convert.ToInt32(Math.Floor(heightNeeded * Convert.ToSingle(resY))));
#endregion
img.Write(folder + thisModel.cropURL);
}