reducing image size
- Hi..
im building an application to distribute the desktop image to other clients...
when i capture the desktop the image size is approximatly 55KB.. in a Jpeg format.. is there a routine or a method to reduce the size?!
thnx
Answers
Hello. I recently ran into this same problem while trying to resize images. While I know of no .NET easy solution to the problem, I modified an image library I found online to fit my needs. I have tested this following code somewhat, but it may contain some bugs as I have not tested it extensively. I made several changes to the code to give different options for scaling an image and a rework of the basic way it resized images.
At any rate, I hope that it at least helps to push you in the right direction. Perhaps someone with more knowledge on the subject will note any flaws they see in the code, or suggest a better solution. I just wanted to help if I could:
using
System;using
System.Collections.Generic;using
System.Text;using
System.Drawing;using
System.Drawing.Imaging;using
System.Drawing.Drawing2D;namespace
Imaging{
public class ImageResize{
private ImageResize() { } //Scale the image to a percentage of its actual size. public static Image ScaleByPercentage(Image img, double percent){
double fractionalPercentage = (percent / 100.0); int outputWidth = (int)(img.Width * fractionalPercentage); int outputHeight = (int)(img.Height * fractionalPercentage); return ImageResize.ScaleImage(img, outputWidth, outputHeight);}
//Scale down the image till it fits the given size. public static Image ScaleDownTillFits(Image img, Size size){
Image ret = img; bool bFound = false; if ((img.Width > size.Width) || (img.Height > size.Height)){
for (double percent = 100; percent > 0; percent--){
double fractionalPercentage = (percent / 100.0); int outputWidth = (int)(img.Width * fractionalPercentage); int outputHeight = (int)(img.Height * fractionalPercentage); if ((outputWidth < size.Width) && (outputHeight < size.Height)){
bFound =
true;ret =
ImageResize.ScaleImage(img, outputWidth, outputHeight); break;}
}
if (!bFound){
ret =
ImageResize.ScaleImage(img, size.Width, size.Height);}
}
return ret;}
//Scale an image by a set width. The height will be set proportionally. public static Image ScaleByWidth(Image img, int width){
double fractionalPercentage = ((double)width / (double)img.Width); int outputWidth = width; int outputHeight = (int)(img.Height * fractionalPercentage); return ImageResize.ScaleImage(img, outputWidth, outputHeight);}
//Scale an image by a set height. The width will be set proportionally. public static Image ScaleByHeight(Image img, int height){
double fractionalPercentage = ((double)height / (double)img.Height); int outputWidth = (int)(img.Width * fractionalPercentage); int outputHeight = height; return ImageResize.ScaleImage(img, outputWidth, outputHeight);}
//Scale an image to a given width and height. public static Image ScaleImage(Image img, int outputWidth, int outputHeight){
Bitmap outputImage = new Bitmap(outputWidth, outputHeight, img.PixelFormat);outputImage.SetResolution(img.HorizontalResolution, img.VerticalResolution);
Graphics graphics = Graphics.FromImage(outputImage);graphics.InterpolationMode =
InterpolationMode.HighQualityBicubic;graphics.DrawImage(img,
new Rectangle(0, 0, outputWidth, outputHeight), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);graphics.Dispose();
return outputImage;}
}
}
All Replies
Hello. I recently ran into this same problem while trying to resize images. While I know of no .NET easy solution to the problem, I modified an image library I found online to fit my needs. I have tested this following code somewhat, but it may contain some bugs as I have not tested it extensively. I made several changes to the code to give different options for scaling an image and a rework of the basic way it resized images.
At any rate, I hope that it at least helps to push you in the right direction. Perhaps someone with more knowledge on the subject will note any flaws they see in the code, or suggest a better solution. I just wanted to help if I could:
using
System;using
System.Collections.Generic;using
System.Text;using
System.Drawing;using
System.Drawing.Imaging;using
System.Drawing.Drawing2D;namespace
Imaging{
public class ImageResize{
private ImageResize() { } //Scale the image to a percentage of its actual size. public static Image ScaleByPercentage(Image img, double percent){
double fractionalPercentage = (percent / 100.0); int outputWidth = (int)(img.Width * fractionalPercentage); int outputHeight = (int)(img.Height * fractionalPercentage); return ImageResize.ScaleImage(img, outputWidth, outputHeight);}
//Scale down the image till it fits the given size. public static Image ScaleDownTillFits(Image img, Size size){
Image ret = img; bool bFound = false; if ((img.Width > size.Width) || (img.Height > size.Height)){
for (double percent = 100; percent > 0; percent--){
double fractionalPercentage = (percent / 100.0); int outputWidth = (int)(img.Width * fractionalPercentage); int outputHeight = (int)(img.Height * fractionalPercentage); if ((outputWidth < size.Width) && (outputHeight < size.Height)){
bFound =
true;ret =
ImageResize.ScaleImage(img, outputWidth, outputHeight); break;}
}
if (!bFound){
ret =
ImageResize.ScaleImage(img, size.Width, size.Height);}
}
return ret;}
//Scale an image by a set width. The height will be set proportionally. public static Image ScaleByWidth(Image img, int width){
double fractionalPercentage = ((double)width / (double)img.Width); int outputWidth = width; int outputHeight = (int)(img.Height * fractionalPercentage); return ImageResize.ScaleImage(img, outputWidth, outputHeight);}
//Scale an image by a set height. The width will be set proportionally. public static Image ScaleByHeight(Image img, int height){
double fractionalPercentage = ((double)height / (double)img.Height); int outputWidth = (int)(img.Width * fractionalPercentage); int outputHeight = height; return ImageResize.ScaleImage(img, outputWidth, outputHeight);}
//Scale an image to a given width and height. public static Image ScaleImage(Image img, int outputWidth, int outputHeight){
Bitmap outputImage = new Bitmap(outputWidth, outputHeight, img.PixelFormat);outputImage.SetResolution(img.HorizontalResolution, img.VerticalResolution);
Graphics graphics = Graphics.FromImage(outputImage);graphics.InterpolationMode =
InterpolationMode.HighQualityBicubic;graphics.DrawImage(img,
new Rectangle(0, 0, outputWidth, outputHeight), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);graphics.Dispose();
return outputImage;}
}
}


