博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图片裁剪 PhotoCropper
阅读量:6291 次
发布时间:2019-06-22

本文共 3138 字,大约阅读时间需要 10 分钟。

 

    

 

 

 

 

 

 

 

 

using System;using System.Web;using System.IO;using System.Web.UI;using System.Drawing;using System.Drawing.Imaging;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;public partial class PhotoCropper : System.Web.UI.Page{    //原图    private const string ORIG_SAMPLE_PHOTO_URL ="photos/2.jpg";    //裁剪图    private const string CROPPED_SAMPLE_PHOTO_URL = "photos/2Cropped.jpg"; protected void Page_Load(object sender, System.EventArgs e){        if (!Page.IsPostBack)        {            loadPhoto(ORIG_SAMPLE_PHOTO_URL);        }        else        {            loadPhoto(CROPPED_SAMPLE_PHOTO_URL);            btnCrop.Visible = !btnCrop.Visible;            btnReset.Visible = !btnReset.Visible;        }}    protected void loadPhoto(string url)     {        imgSample.ImageUrl = url;    }    protected void btnCrop_Click(object sender, EventArgs e)    {        int iWidth = Convert.ToInt16(width.Value);        int iHeight =  Convert.ToInt16(height.Value);        int iX =  Convert.ToInt16(x1.Value);        int iY =  Convert.ToInt16(y1.Value);        //用字节流读取        byte[] rawData = File.ReadAllBytes(Context.Server.MapPath(""+ORIG_SAMPLE_PHOTO_URL+""));        byte[] newImage = CropImageFile(rawData, iWidth, iHeight, iX, iY);          writeByteArrayToFile(newImage);    }    //重置    protected void btnReset_Click(object sender, EventArgs e)    {        Response.Redirect("PhotoCropper.aspx", true);    }    //字节数组换成图片文件
protected void writeByteArrayToFile(byte[] byteImage) {        using (BinaryWriter binWriter =        new BinaryWriter(File.Open(Context.Server.MapPath("" + CROPPED_SAMPLE_PHOTO_URL + ""), FileMode.Create)))        {            binWriter.Write(byteImage);        }    }    //裁剪    protected byte[] CropImageFile(byte[] imageFile, int targetW, int targetH, int targetX, int targetY)    {        MemoryStream imgMemoryStream = new MemoryStream();        System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(new MemoryStream(imageFile));        Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);        bmPhoto.SetResolution(72, 72);           Graphics grPhoto = Graphics.FromImage(bmPhoto);        grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;        grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;        grPhoto.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;        try        {            grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), targetX, targetY, targetW, targetH, GraphicsUnit.Pixel);            bmPhoto.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);        }        catch (Exception e)        {            throw e;        }        finally        {            imgPhoto.Dispose();            bmPhoto.Dispose();            grPhoto.Dispose();        }        return imgMemoryStream.GetBuffer();    }    //转换图片成子节流
protected byteImage bytetry        using MemoryStream new MemoryStreamImageFormatcatch Exception throw return

}

本文转自曾祥展博客园博客,原文链接:http://www.cnblogs.com/zengxiangzhan/archive/2010/01/04/1638780.html,如需转载请自行联系原作者

你可能感兴趣的文章
String 源码浅析(一)
查看>>
Spring Boot 最佳实践(三)模板引擎FreeMarker集成
查看>>
Fescar 发布 0.2.3 版本,支持 Redis 和 Apollo
查看>>
Google MapReduce到底解决什么问题?
查看>>
CCNP-6 OSPF试验2(BSCI)
查看>>
Excel 2013 全新的图表体验
查看>>
openstack 制作大于2TB根分区自动扩容的CENTOS镜像
查看>>
Unbuntu安装遭遇 vmware上的Easy install模式
查看>>
几个常用的ASP木马
查看>>
python分析postfix邮件日志的状态
查看>>
Mysql-5.6.x多实例配置
查看>>
psutil
查看>>
在git@osc上托管自己的代码
查看>>
机器学习算法:朴素贝叶斯
查看>>
小五思科技术学习笔记之扩展访问列表
查看>>
使用Python脚本检验文件系统数据完整性
查看>>
使用MDT部署Windows Server 2003 R2
查看>>
Redhat as5安装Mysql5.0.28
查看>>
通过TMG发布ActiveSync
查看>>
Web服务器的配置与管理(4) 配置访问权限和安全
查看>>