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

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

 

    

 

 

 

 

 

 

 

 

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 byte[] ConvertImageToByteArray(System.Drawing.Image imageToConvert )      {        byte[] imgByteArray;        try        {            using (MemoryStream imgMemoryStream = new MemoryStream())            {                               imageToConvert.Save(imgMemoryStream, ImageFormat.Jpeg);                imgByteArray = imgMemoryStream.ToArray();            }        }        catch (Exception e)        {            throw e;        }        return imgByteArray;    }}
    本文转自曾祥展博客园博客,原文链接:http://www.cnblogs.com/zengxiangzhan/archive/2010/01/04/1638780.html,如需转载请自行联系原作者
你可能感兴趣的文章
文件类似的推理 -- 超级本征值(super feature)
查看>>
【XCode7+iOS9】http网路连接请求、MKPinAnnotationView自定义图片和BitCode相关错误--备用...
查看>>
各大公司容器云的技术栈对比
查看>>
记一次eclipse无法启动的排查过程
查看>>
【转】jmeter 进行java request测试
查看>>
读书笔记--MapReduce 适用场景 及 常见应用
查看>>
SignalR在Xamarin Android中的使用
查看>>
走过电竞之路的程序员
查看>>
Eclipse和MyEclipse使用技巧--Eclipse中使用Git-让版本管理更简单
查看>>
[转]响应式表格jQuery插件 – Responsive tables
查看>>
8个3D视觉效果的HTML5动画欣赏
查看>>
C#如何在DataGridViewCell中自定义脚本编辑器
查看>>
【linux】crontab定时命令
查看>>
Android UI优化——include、merge 、ViewStub
查看>>
Office WORD如何取消开始工作右侧栏
查看>>
Android Jni调用浅述
查看>>
CodeCombat森林关卡Python代码
查看>>
第一个应用程序HelloWorld
查看>>
(二)Spring Boot 起步入门(翻译自Spring Boot官方教程文档)1.5.9.RELEASE
查看>>
Android Annotation扫盲笔记
查看>>