<script language="JavaScript" >
var flag=false;
function changeImg(ImgD,iwidth,iheight){ var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){ flag=true;
if(image.width/image.height>= iwidth/iheight){ if(image.width>iwidth){ ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{ ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{ if(image.height>iheight){ ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{ ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
}
}
</script>
public static byte[] ResizeImageFile(byte[] imageFile, int targetSizeW, int targetSizeH)
{ System.Drawing.Image original = System.Drawing.Image.FromStream(new MemoryStream(imageFile));
int targetH, targetW;
targetW = targetSizeW;
targetH = (int)(original.Height * ((float)targetSizeW / (float)original.Width));
if (targetH > targetSizeH)
{ targetH = targetSizeH;
targetW = (int)(original.Width * ((float)targetSizeH / (float)original.Height));
}
if (targetSizeW < (int)original.Width || targetSizeH < (int)original.Height)
{ System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(new MemoryStream(imageFile));
// Create a new blank canvas. The resized image will be drawn on this canvas.
Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(72, 72);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);
// Save out to memory and then to a file. We dispose of all objects to make sure the files don't stay locked.
MemoryStream mm = new MemoryStream();
bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
original.Dispose();
imgPhoto.Dispose();
bmPhoto.Dispose();
grPhoto.Dispose();
return mm.GetBuffer();
}
else
{ return imageFile;
}
}
- 评论:(0)
发表评论 点击这里获取该日志的TrackBack引用地址