mercredi 1 octobre 2014

Image resizing at different levels creating different size images


Vote count:

0




I have the following code placed at a button click. Button got clicked after user selects an image to upload.



using (SD.Image OriginalImage = SD.Image.FromStream(AsyncUpload.FileContent))
{
decimal dRatio = (Decimal)1024 / OriginalImage.Width;
int iScaledW = 1024;
int iScaledH = (int)(OriginalImage.Height * dRatio);
using (SD.Bitmap bmp = new SD.Bitmap(iScaledW, iScaledH))
{
bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
using (SD.Graphics Graphic = SD.Graphics.FromImage(bmp))
{
Graphic.SmoothingMode =SD2.SmoothingMode.AntiAlias;
Graphic.InterpolationMode = SD2.InterpolationMode.HighQualityBicubic;
Graphic.PixelOffsetMode = SD2.PixelOffsetMode.HighQuality;
Graphic.FillRectangle(SD.Brushes.White, 0, 0, iScaledW, iScaledH);
Graphic.DrawImage(OriginalImage, 0, 0, iScaledW, iScaledH);
bmp.Save(Path.Combine(Server.MapPath(upPath), AsyncUpload.FileName));
}
}
}


Here AsyncUpload is the name of FileUpload control. Original image is 3.96MB and by re-sizing the image with the above function, creates a re-sized image of 1.92MB.


However, If I select that same 3.96MB file as click a button that executes the following code that simply uploads whatever provided.



objfile.SaveAs(Path.Combine(Server.MapPath(upPath), AsyncUpload.FileName));


And then click a button that do the same processing of re-sizing the image as above mentioned, it creates an image of 200KB only. Below is the code of that button click.



static byte[] Crop(string Img, int Width, int Height, int X, int Y)
{
int w = Width;
int h = Height;
int xx = X;
int yy = Y;

try
{
using (SD.Image OriginalImage = SD.Image.FromFile(Img))
{
if (w == 0 || h == 0)
{
w = OriginalImage.Width;
h = OriginalImage.Height;
}

int iScaleX = 0, iScaleY = 0;
GetImageResolution(w, h, out iScaleX, out iScaleY);
using (SD.Bitmap bmp = new SD.Bitmap(iScaleX, iScaleY))
{
bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
using (SD.Graphics Graphic = SD.Graphics.FromImage(bmp))
{
Graphic.SmoothingMode = SmoothingMode.AntiAlias;
Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
Graphic.FillRectangle(SD.Brushes.White, 0, 0, iScaleX, iScaleY);
Graphic.DrawImage(OriginalImage, 0, 0, iScaleX, iScaleY);
bmp.Save(<Path>);
}

}

}

}
catch (Exception Ex)
{
return null;
// throw (Ex);
}

}


Both the code are same but the output images are of different sizes and that too, a big difference in size. Please help me understand why is this behavior. I want to avoid uploading big size image by reading them as stream and re-sizing them before even uploading them to disk. But the size after re-sizing is too big to accept.


Also, if anybody can share their thoughts of how to compress and image.


Thanks in advance



asked 1 min ago







Image resizing at different levels creating different size images

Aucun commentaire:

Enregistrer un commentaire