Thursday, September 24, 2009

Changing Image Scale And Resolution - ASP.NET

Storing image on a optimized size and resolution(network administrator sense it) or transfering optimized images on a limited network capacity allways affect the application performace(Network administrator and all Users sense it).


using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace ChangingImage
{
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 < bfound =" true;" ret =" ImageResize.ScaleImage(img," ret =" ImageResize.ScaleImage(img," color="#33cc00">//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 by a set Height and set Specific Resolution. The width will be set proportionally.
public static Image ScaleByHeightAndResolution(Image img, int height,float Resolution)
{
double fractionalPercentage = ((double)height / (double)img.Height);
int outputWidth = (int)(img.Width * fractionalPercentage);
int outputHeight = height;
return ImageResize.ScaleImageAndResolution(img, outputWidth, outputHeight,Resolution);
}
//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.Bilinear;
graphics.DrawImage(img, new Rectangle(0, 0, outputWidth, outputHeight), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
graphics.Dispose();
return outputImage;
}

//Change image scale by Width and Height and Set New Resoltion too.
public static Image ScaleImageAndResolution(Image img, int outputWidth, int outputHeight,float Resolution)
{
Bitmap outputImage = new Bitmap(outputWidth, outputHeight, img.PixelFormat);
outputImage.SetResolution(Resolution, Resolution);
Graphics graphics = Graphics.FromImage(outputImage);
Graphics.InterpolationMode = InterpolationMode.Bilinear;
graphics.DrawImage(img, new Rectangle(0, 0, outputWidth, outputHeight), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
graphics.Dispose();
return outputImage;
}
}
}

1 comment:

  1. Bitmap outputImage = new Bitmap(outputWidth, outputHeight, img.PixelFormat);
    outputImage.SetResolution(Resolution, Resolution);
    Graphics graphics = Graphics.FromImage(outputImage);
    Graphics.InterpolationMode = InterpolationMode.Bilinear;
    graphics.DrawImage(img, new Rectangle(0, 0, outputWidth, outputHeight), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
    graphics.Dispose();
    return outputImage;

    ReplyDelete

Thank you for sharing your knowledge and experiences with this weblog.