您的位置:首页 > 运维架构

裁剪图片CropImg的一个小例子【N年前得】

2011-03-03 05:37 323 查看

网上有很多这样的裁剪图片,只是都不完善 。我里面有个demo。js---裁剪---c#生成图片---存放。最后想加个压缩图片的。但是

发现不太明显。就送一个压缩图片的利器。

View Code

using System;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
//using System.Text;

namespace WebApplication1
{
public partial class test : System.Web.UI.Page
{
public static int pPartStartPointX;
public static int pPartStartPointY;
public static int pPartWidth;
public static int pPartHeight;

protected void Page_Load(object sender, EventArgs e)
{

}

public void tesst(object sender, EventArgs e)
{
pPartStartPointX = int.Parse(Request.Form["x"]);
pPartStartPointY = int.Parse(Request.Form["y"].ToString());
pPartWidth = int.Parse(Request.Form["w"].ToString());
pPartHeight = int.Parse(Request.Form["h"].ToString());

string originalPath = "b.jpg";
string savePath = HttpContext.Current.Server.MapPath("~/Images/CropImg/");
string filename = CropImage(originalPath, savePath, pPartWidth, pPartHeight, pPartStartPointX, pPartStartPointY);
string fullpath = "../Images/CropImg/" + filename;
}

public static string CropImage(string originamImgPath, string imgPath, int width, int height, int x, int y)
{
string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
byte[] CropImage = Crop(originamImgPath, width, height, x, y);
using (MemoryStream ms = new MemoryStream(CropImage, 0, CropImage.Length))
{
ms.Write(CropImage, 0, CropImage.Length);
using (System.Drawing.Image CroppedImage = System.Drawing.Image.FromStream(ms, true))
{
string SaveTo = imgPath + filename;
CroppedImage.Save(SaveTo, CroppedImage.RawFormat);
}
}
// MakeThumbnail("Images/CropImg/20110112153907.jpg", "Images/CropImg/00000.jpg", 387, 326, "Cut");
return filename;
}

private static byte[] Crop(string Img, int Width, int Height, int X, int Y)
{
try
{
using (Image OriginalImage = Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(Img)))
{
using (Bitmap bmp = new Bitmap(Width, Height, OriginalImage.PixelFormat))
{
bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);
using (Graphics Graphic = Graphics.FromImage(bmp))
{
Graphic.SmoothingMode = SmoothingMode.AntiAlias;
Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
Graphic.DrawImage(OriginalImage, new Rectangle(0, 0, Width, Height), X, Y, Width, Height, GraphicsUnit.Pixel);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, OriginalImage.RawFormat);
return ms.GetBuffer();
}
}
}
}
catch (Exception Ex)
{
throw (Ex);
}
}
}
}


原理就是或者js得到的坐标。长宽高一类的东西。然后用c#的画图类、操作。裁剪图片。生成一个新图片。存放到一个新地方。里面的js的jquery.Jcrop.min自己搜着下载吧。。

这个,demo。还没发现咋上传呢。有空上传上去

还有个压缩工具ImageOptimizer510 。。很完美。我的压缩图片。一直压缩质量不完美。。有空改善下吧。这个工具很强

]]>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: