当前位置:首页 > 通信资讯 > 正文

C# 图片处理(C# 保存图片)

最近需要将一张图片上传并按指定位置剪裁,后来在网上找了一个剪裁图片的插件,但是只有前台没有后端,然后我各种百度,并最终完成,特此写一篇博客略表纪念。

前台我就不说了,用的cropper插件,有兴趣的自己去百度找找吧。服务器之家 有这个插件。

下面是代码:

?
1 2 3 4 5 6 7 8 9 10 11 HttpPostedFile file = context.Request.Files["avatar_file"]; string datasize = context.Request.Params["avatar_data"]; //{"x":30.003846153846148,"y":16.715384615384625,"height":300.8,"width":300.8,"rotate":0} 剪裁之后参数 JavaScriptSerializer jss = new JavaScriptSerializer(); ImgSize imagesize = jss.Deserialize<ImgSize>(datasize); byte[] FileByte = SetFileToByteArray(file);//图片数组 string strtExtension = System.IO.Path.GetExtension(file.FileName);//图片格式 MemoryStream ms1 = new MemoryStream(FileByte); Bitmap sBitmap = (Bitmap)Image.FromStream(ms1); Rectangle section = new Rectangle(new Point(imagesize.ToInt(imagesize.x), imagesize.ToInt(imagesize.y)), new Size(imagesize.ToInt(imagesize.width), imagesize.ToInt(imagesize.height))); Bitmap CroppedImage = MakeThumbnailImage(sBitmap, section.Width, section.Height, section.Width, section.Height, section.X, section.Y);

上面代码中用到我自己创建了一个ImgSize类,代码如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 class ImgSize { //{"x":30.003846153846148,"y":16.715384615384625,"height":300.8,"width":300.8,"rotate":0} public double x { get; set; } public double y { get; set; } public double width { get; set; } public double height { get; set; } public int rotate { get; set; } public int ToInt(double doubleValue) { return Convert.ToInt32(doubleValue); } }

上面代码中使用到的几个方法:

文件转化:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 /// <summary> /// 将From表单file文件转化为byte数组 /// </summary> /// <param name="File">from提交文件流</param> /// <returns></returns> private byte[] SetFileToByteArray(HttpPostedFile File) { Stream stream = File.InputStream; byte[] AyyayByte = new byte[File.ContentLength]; stream.Read(AyyayByte, 0, File.ContentLength); stream.Close(); return AyyayByte; }

核心剪裁方法:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 /// <summary> /// 裁剪图片并保存 /// </summary> /// <param name="Image">图片信息</param> /// <param name="maxWidth">缩略图宽度</param> /// <param name="maxHeight">缩略图高度</param> /// <param name="cropWidth">裁剪宽度</param> /// <param name="cropHeight">裁剪高度</param> /// <param name="X">X轴</param> /// <param name="Y">Y轴</param> public static Bitmap MakeThumbnailImage(Image originalImage, int maxWidth, int maxHeight, int cropWidth, int cropHeight, int X, int Y) { Bitmap b = new Bitmap(cropWidth, cropHeight); try { using (Graphics g = Graphics.FromImage(b)) { //清空画布并以透明背景色填充 g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, new Rectangle(0, 0, cropWidth, cropHeight), X, Y, cropWidth, cropHeight, GraphicsUnit.Pixel); Image displayImage = new Bitmap(b, maxWidth, maxHeight); displayImage.Save("E:\\cutimg.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); Bitmap bit = new Bitmap(b, maxWidth, maxHeight); return bit; } } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); b.Dispose(); } }

最后的结果是把存到了E盘根目录下面

以上所述是小编给大家介绍的C#如何实现图片的剪裁并保存,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的,在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.cnblogs.com/zqt14520/archive/2016/11/11/6054621.html

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。