积极答复者
怎样使用ASP.Net剪切图片,根据原图Width、原图Height、需求的Top坐标、需求Left坐标、需求的Width、需求的Height?

问题
答案
-
原理就是:客户端使用js选择区域。将这些数据提交,然后服务器gdi+切割
private static Image cropImage( Image img, Rectangle cropArea)
{
Bitmap bmpImage = new Bitmap ( img) ;
Bitmap bmpCrop = bmpImage.Clone ( cropArea, bmpImage.PixelFormat ) ;
return ( Image ) ( bmpCrop) ;
}
【孟子E章】- 已建议为答案 肖小勇Moderator 2009年10月21日 1:02
- 已标记为答案 艾边成 2009年10月21日 2:52
全部回复
-
原理就是:客户端使用js选择区域。将这些数据提交,然后服务器gdi+切割
private static Image cropImage( Image img, Rectangle cropArea)
{
Bitmap bmpImage = new Bitmap ( img) ;
Bitmap bmpCrop = bmpImage.Clone ( cropArea, bmpImage.PixelFormat ) ;
return ( Image ) ( bmpCrop) ;
}
【孟子E章】- 已建议为答案 肖小勇Moderator 2009年10月21日 1:02
- 已标记为答案 艾边成 2009年10月21日 2:52
-
网上有很多例子
//Namespace Reference
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
/// <summary>
/// method for cropping an image.
/// </summary>
/// <param name="img">the image to crop</param>
/// <param name="width">new height</param>
/// <param name="height">new width</param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public Image Crop(string img, int width, int height, int x, int y)
{
try
{
Image image = Image.FromFile(img);
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
bmp.SetResolution(80, 60);
Graphics gfx = Graphics.FromImage(bmp);
gfx.SmoothingMode = SmoothingMode.AntiAlias;
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
gfx.DrawImage(image, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
// Dispose to free up resources
image.Dispose();
bmp.Dispose();
gfx.Dispose();
return bmp;
}
catch (Exception ex)
{
return null;
}
}
【孟子E章】 -
我这样调用,怎么不能将图片保存到服务器的硬盘呢?
string physicalImagePath = Server.MapPath("~/images/users/1001.jpg"); FileStream fs = new FileStream(Server.MapPath("~/images/users/AAA.jpg"), FileMode.Create, FileAccess.Write); System.Drawing.Image resultImage = Crop(physicalImagePath, imageWidth, imageHeight, imageLeft, imageTop); resultImage.Save(fs,System.Drawing.Imaging.ImageFormat.Jpeg); resultImage.Dispose();
请高手指点?
努力!奋斗