在我制作的应用程序中,有必要创建一个具有多个选项的函数。它很大,但它会调整图像大小,可以保持纵横比,并且可以切割边缘以仅返回图像的中心:
/// <summary>
/// Resize image with an URL as source
/// </summary>
/// <param name="OriginalFileURL">Link to the image</param>
/// <param name="heigth">new height</param>
/// <param name="width">new width</param>
/// <param name="keepAspectRatio">keep the aspect ratio</param>
/// <param name="getCenter">return the center bit of the image</param>
/// <returns>image with new dimentions</returns>
public Image resizeImageFromURL(String OriginalFileURL, int heigth, int width, Boolean keepAspectRatio, Boolean getCenter)
{
int newheigth = heigth;
WebResponse response = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(OriginalFileURL);
response = request.GetResponse();
}
catch
{
return (System.Drawing.Image) new Bitmap(1, 1);
}
Stream imageStream = response.GetResponseStream();
System.Drawing.Image FullsizeImage = System.Drawing.Image.FromStream(imageStream);
// Prevent using images internal thumbnail
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
if (keepAspectRatio || getCenter)
{
int bmpY = 0;
double resize = (double)FullsizeImage.Width / (double)width;//get the resize vector
if (getCenter)
{
bmpY = (int)((FullsizeImage.Height - (heigth * resize)) / 2);// gives the Y value of the part that will be cut off, to show only the part in the center
Rectangle section = new Rectangle(new System.Drawing.Point(0, bmpY), new System.Drawing.Size(FullsizeImage.Width, (int)(heigth * resize)));// create the section to cut of the original image
Bitmap orImg = new Bitmap((Bitmap)FullsizeImage);//for the correct effect convert image to bitmap.
FullsizeImage.Dispose();//clear the original image
using (Bitmap tempImg = new Bitmap(section.Width, section.Height))
{
Graphics cutImg = Graphics.FromImage(tempImg);// set the file to save the new image to.
cutImg.DrawImage(orImg, 0, 0, section, GraphicsUnit.Pixel);// cut the image and save it to tempImg
FullsizeImage = tempImg;//save the tempImg as FullsizeImage for resizing later
orImg.Dispose();
cutImg.Dispose();
return FullsizeImage.GetThumbnailImage(width, heigth, null, IntPtr.Zero);
}
}
else newheigth = (int)(FullsizeImage.Height / resize);// set the new heigth of the current image
}//return the image resized to the given heigth and width
return FullsizeImage.GetThumbnailImage(width, newheigth, null, IntPtr.Zero);
}
为了更容易访问该函数,可以添加一些重载函数:
/// <summary>
/// Resize image with an URL as source
/// </summary>
/// <param name="OriginalFileURL">Link to the image</param>
/// <param name="heigth">new height</param>
/// <param name="width">new width</param>
/// <returns>image with new dimentions</returns>
public Image resizeImageFromURL(String OriginalFileURL, int heigth, int width)
{
return resizeImageFromURL(OriginalFileURL, heigth, width, false, false);
}
/// <summary>
/// Resize image with an URL as source
/// </summary>
/// <param name="OriginalFileURL">Link to the image</param>
/// <param name="heigth">new height</param>
/// <param name="width">new width</param>
/// <param name="keepAspectRatio">keep the aspect ratio</param>
/// <returns>image with new dimentions</returns>
public Image resizeImageFromURL(String OriginalFileURL, int heigth, int width, Boolean keepAspectRatio)
{
return resizeImageFromURL(OriginalFileURL, heigth, width, keepAspectRatio, false);
}
现在是可选设置的最后两个布尔值。像这样调用函数:
System.Drawing.Image ResizedImage = resizeImageFromURL(LinkToPicture, 800, 400, true, true);