How do i change batch of gif's files resolution? - c#

I created animated gif file from many small gif's.
I know the animated gif resolution is 512x512
Now i want to create the same animated gif from th same gif's but before i create the animate gif i want first to change each gif file resolution from 512x512 to 1024x1024
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < myGifList.Count; i++)
{
}
}
In the comboBox1 i have for now only one select a string of "1024x1024". In the constructor of form1 i did:
comboBox1.Items.Add("1024x1024");
In the SelectedIndexChanged event i loop over the file for example the first index in myGifList contain:
C:\Users\bout0_000\AppData\Local\mws\My Weather Station\radar_temp_directory\radar006363.Gif
The second index contain:
C:\Users\bout0_000\AppData\Local\mws\My Weather Station\radar_temp_directory\radar006363.Gif
Now i want to change each file name from 512x512 to 1024x1024. So if the file radar006363.gif wis now 512x512 change it to 1024x1024 and do it fro all the files in myGifList.
myGifList is List
I want to make like batch resolution convertor for all the files in myGifList from 512x512 to 1024x1024.
Edit:
This is what i tried now:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < myGifList.Count; i++)
{
Image img = Image.FromFile(myGifList[i]);
resizeImage(1024, 1024, myGifList[i]);
img.Save(#"c:\temp\newimages\" + Path.GetFileName(myGifList[i]));
}
unfreez.MakeGIF(myGifList, previewFileName, 8, true);
}
And the resizeImage method:
public Image resizeImage(int newWidth, int newHeight, string stPhotoPath)
{
Image imgPhoto = Image.FromFile(stPhotoPath);
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
//Consider vertical pics
if (sourceWidth < sourceHeight)
{
int buff = newWidth;
newWidth = newHeight;
newHeight = buff;
}
int sourceX = 0, sourceY = 0, destX = 0, destY = 0;
float nPercent = 0, nPercentW = 0, nPercentH = 0;
nPercentW = ((float)newWidth / (float)sourceWidth);
nPercentH = ((float)newHeight / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((newWidth -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((newHeight -
(sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(newWidth, newHeight,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Black);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
But the file that saved is still 512x512

Related

Resize Image But Wrong Border Color (Black 'Auto')

public Image resizeImage(int newWidth, int newHeight, string stPhotoPath)
{
Image imgPhoto = Image.FromFile(stPhotoPath);
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
if (sourceWidth < sourceHeight)
{
int buff = newWidth;
newWidth = newHeight;
newHeight = buff;
}
int sourceX = 0, sourceY = 0, destX = 0, destY = 0;
float nPercent = 0, nPercentW = 0, nPercentH = 0;
nPercentW = ((float)newWidth / (float)sourceWidth);
nPercentH = ((float)newHeight / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((newWidth -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((newHeight -
(sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(newWidth, newHeight,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Black);
grPhoto.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
imgPhoto.Dispose();
return bmPhoto;
}
I use such a code to scale down images, but there is a problem that after the images are scaled down, the remaining spaces remain black. Something that should definitely not happen. What is the solution? The background color must be white.
public static class ResizeImage
{
/// <summary>
/// Return new resized size to display the image
/// </summary>
/// <param name="srcrectanle">source rectangle of image or you can pass the
bitmap and set the size accrodingly</param>
/// <param name="initSize">initial size of the page to draw image</param>
/// <returns></returns>
public static SizeF getResizedRectangle(RectangleF srcrectanle, SizeF initSize)
{
float sw = srcrectanle.Width;
float sh = srcrectanle.Height;
float dw = initSize.Width;
float dh = initSize.Height;
float finalHeight, finalWidth;
float Sourceratio = sw / sh;
if (Sourceratio >= 1)
{
finalWidth = (int)dw;
float ratio = sw / dw;
finalHeight = (sh / ratio);
}
else
{
finalHeight = (int)dh;
float ratio = sh / dh;
finalWidth = (sw / ratio);
}
return new SizeF(finalHeight, finalHeight);
}
}
Use this function to resize your image. Hope you have the knowledge of source rectangle and destination rectangle.

Image Getting Stretched when trying to Resize It keeping the aspect Ratio

Im using the following code to resize an image preserving the aspect ratio
public Bitmap resizeImage(System.Drawing.Image imgToResize, SizeF size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((System.Drawing.Image)b);
// Used to Prevent White Line Border
// g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return b;
}
But images with large width gets compressed and the contents seem packed up into a small space.What im trying to achieve is this : Resize the Large Size/Large Resolution image because processing this will take huge time,So when the image width or height exceeds say 1000 i want to resize the image to a Smaller size eg:1000 Width or Height which ever is larger,so that i can save computation time.But it seems the above code is forcibly trying to fit the image into the 1000X1000 Box when i do
if (y.Width > 1000 || y.Height > 1000)
{
y = new Bitmap( resizeImage(y, new Size(1000, 1000)));
}
Try this, its a bit neater
public static Bitmap ResizeImage(Bitmap source, Size size)
{
var scale = Math.Min(size.Width / (double)source.Width, size.Height / (double)source.Height);
var bmp = new Bitmap((int)(source.Width * scale), (int)(source.Height * scale));
using (var graph = Graphics.FromImage(bmp))
{
graph.InterpolationMode = InterpolationMode.High;
graph.CompositingQuality = CompositingQuality.HighQuality;
graph.SmoothingMode = SmoothingMode.AntiAlias;
graph.DrawImage(source, 0, 0, bmp.Width, bmp.Height);
}
return bmp;
}

Resize Image Keep Aspect Ratio White Line Border

Im using the following code (adapted from Example #2 here ) to Resize an Image keeping the aspect ratio.But i keep getting a white border around the resized images.What im i doing wrong.
Bitmap ResizekeepAspectRatio(Bitmap imgPhoto, int Width, int Height)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((Width -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((Height -
(sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(Width, Height,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.White);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
UPDATE:
Sample Resized Image
Zooming in Corner Reveals White Border
Try to change the line
Bitmap bmPhoto = new Bitmap(Width, Height,
PixelFormat.Format24bppRgb);
To
Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
PixelFormat.Format24bppRgb);
Since you want to keep the aspect ratio most of the time you will end up having extra space around the image so if you don't need that extra space then make the new picture size fit to the new destination size
Edit:
Try to comment out the line grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic

Translate/Detect Rectangle portion of Image from Resized Image

I have a Large size image.Since it takes long to process high res images i resize it keeping the aspect ratio.From the resized image i detect a rectangle and i have the coordinates of the rectangle.
Bitmap ResizekeepAspectRatio(Bitmap imgPhoto, int Width, int Height)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((Width -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((Height -
(sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(Width, Height,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Red);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
Is there a way by which i can translate/Map this rectangle to the Large image so that i get the same area.Im doing this to save time.
Some clarification:
I have a Large Original Image.. i resize it keeping the aspect ratio and using some processing i get a rectangle portion in it( Just the coordinates).Since the Image quality of this portion is not good i need to find a way to map this coordinate to the large image.
Ok, so if I understand it clear here it is:
You have a viewport in what you select a rectangle and you want to scale this rectangle to the unscaled image.
So we will have a function like this:
public RectangleF TranslateScale(RectangleF CropRectangle, Bitmap imgPhoto)
First of all what we need is to calculate the multiplier to fit the image on the viewport, exactly like your function:
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
Now that we know the scale percentage we just take the inverse of your function, instead of multiply just divide the rectangle size and position:
CropRectangle.X /= nPercent;
CropRectangle.Y /= nPercent;
CropRectangle.Width /= nPercent;
CropRectangle.Height /= nPercent
return CropRectangle;
And that's it, now you have the rectangle scaled to the original image size, you now can crop that rectangle.

Resizing image get damage in pixel

My requirement is to resize an image if it is bigger than 800 X 600 pixels. I have to resize it to 800 X 600 resolution as per aspect ratio.
I am using below code to re-size it:
public string ResizeUserImage(string fullFileName, int maxHeight, int maxWidth, string newFileName)
{
string savepath = System.Web.HttpContext.Current.Server.MapPath("//InitiativeImage//");
try
{
using (Image originalImage = Image.FromFile(fullFileName))
{
int height = originalImage.Height;
int width = originalImage.Width;
int newHeight = maxHeight;
int newWidth = maxWidth;
if (height > maxHeight || width > maxWidth)
{
if (height > maxHeight)
{
newHeight = maxHeight;
float temp = ((float)width / (float)height) * (float)maxHeight;
newWidth = Convert.ToInt32(temp);
height = newHeight;
width = newWidth;
}
if (width > maxWidth)
{
newWidth = maxWidth;
float temp = ((float)height / (float)width) * (float)maxWidth;
newHeight = Convert.ToInt32(temp);
}
Image.GetThumbnailImageAbort abort = new Image.GetThumbnailImageAbort(ThumbnailCallback);
using (Image resizedImage = originalImage.GetThumbnailImage(newWidth, newHeight, abort, System.IntPtr.Zero))
{
//When image is compress then store the image
var guid = Guid.NewGuid().ToString();
string latestFileName = guid + Path.GetExtension(newFileName);
resizedImage.Save(savepath + #"\" + latestFileName);
string finalPath = AppSettings.Domain + "InitiativeImage/" + latestFileName;
return finalPath;
}
}
else if (fullFileName != newFileName)
{
//var guid = Guid.NewGuid().ToString();
//newFileName = guid + Path.GetExtension(newFileName);
//// no resizing necessary, but need to create new file
//originalImage.Save(savepath + #"\" + newFileName);
//string finalPath = AppSettings.Domain + "UserImage/" + newFileName;
return newFileName;
}
return fullFileName;
}
}
catch (Exception)
{
throw;
}
}
It works perfect but resizing image gets damaged in pixel.
See below original image which I am uploading to the server:
After re-sizing it, it is damages like below image:
Hope you got my question. The resized image get damage in pixel. Please let me know where I am wrong or where is the issue.
Pls try out below function.
public void FixedSize(string oldImageFile, int Width, int Height,string finalpath)
{
Bitmap imgPhoto = new Bitmap(oldImageFile);
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width / (float)sourceWidth);
nPercentH = ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = System.Convert.ToInt16((Width -
(sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = System.Convert.ToInt16((Height -
(sourceHeight * nPercent)) / 2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.White);
grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(0, 0, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
bmPhoto.Save(finalpath);
}
From the documentation on Image.GetThumbnailImage method:
The GetThumbnailImage method works well when the requested thumbnail image has a size of about 120 x 120 pixels. If you request a large thumbnail image (for example, 300 x 300) from an Image that has an embedded thumbnail, there could be a noticeable loss of quality in the thumbnail image. It might be better to scale the main image (instead of scaling the embedded thumbnail) by calling the DrawImage method.
But your objective size is around 800x600 pixels, which is way above the indented use.
Thus, I recommend you to take a look at this answer about image resizing.

Categories