裁剪图片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 。。很完美。我的压缩图片。一直压缩质量不完美。。有空改善下吧。这个工具很强
]]>
相关文章推荐
- 一个简单的图片裁剪Activity:android-crop
- 一个图片缩放和裁剪的例子
- 一个简单的WebSerivce传递图片的例子(转贴)
- 纯PHP生成的一个树叶图片画图例子
- 图片裁剪工具(compile 'com.soundcloud.android.crop:lib_crop:1.0.0')的使用!
- 自制一个H5图片拖拽、裁剪插件(原生JS)
- 利用jquery的imgAreaSelect插件实现图片裁剪示例
- 一个sqlite的例子,将图片存入sqlite数据库
- jQuery-图片上传裁剪插件--imgAreaSelect(分析一) 放大缩小
- 一个点击 图片选中,改变图片样式, 传数据到后台的一个例子
- imgareaselect+java实现图片裁剪保存
- div 里边只包含一个img, 结果div的高度比img图片的高度多3px
- SimpleCropView 裁剪图片
- 一个随手指滑动转动图片的例子
- 一个简单的图片展示例子
- base64字符串转文件,以及ngImgCrop裁剪图片并上传保存到服务器示例
- ASP.NET 你肯定会用到的图片裁剪功能,可按长度或宽度裁剪,也能绘制一个更大图
- picasso 将图片裁剪成一个正方形
- SilvetLight 实现的一个上传图片时功能动态裁剪部分区域和缩放图片的功能
- android开发游记:CROP 大图片裁剪原理分析