图片裁剪 PhotoCropper

简介:

曾祥展

曾祥展

曾祥展

 

<script src="js/prototype.js" type="text/javascript"></script>    
<script src="js/scriptaculous.js?load=builder,dragdrop" type="text/javascript"></script>
<script src="js/cropper.js" type="text/javascript"></script>
<script type="text/javascript">
function onEndCrop( coords, dimensions ) {
    $( '<%=x1.ClientID%>' ).value = coords.x1;
    $( '<%=y1.ClientID%>' ).value = coords.y1;
    $( '<%=x2.ClientID%>' ).value = coords.x2;
    $( '<%=y2.ClientID%>' ).value = coords.y2;
    $( '<%=width.ClientID%>' ).value = dimensions.width;
    $( '<%=height.ClientID%>' ).value = dimensions.height;
}
Event.observe( 
    window, 
    'load', 
    function() { 
        new Cropper.ImgWithPreview( 
            '<%=imgSample.ClientID%>',
            {
                minWidth: 67, 
                minHeight: 86,
                onEndCrop: onEndCrop,
                displayOnInit: true 
            }
        ) 
    }
);         
</script>

 

 

 

    <div>  
        <asp:Image id="imgSample" runat="server" />
        <div id="previewArea"></div>
        <asp:Button ID="btnCrop" runat="server" Text="Crop Image" OnClick="btnCrop_Click" />
        <asp:Button ID="btnReset" runat="server" Text="Restart Demo" Visible="false" OnClick="btnReset_Click" />
         <br />    
        <input type="text" name="x1" id="x1" runat="server" style="visibility:hidden;"/>
        <input type="text" name="y1" id="y1" runat="server" style="visibility:hidden;"/>
        <input type="text" name="x2" id="x2" runat="server" style="visibility:hidden;"/>
        <input type="text" name="y2" id="y2" runat="server" style="visibility:hidden;"/>
        <input type="text" name="width" id="width" runat="server" style="visibility:hidden;"/>
        <input type="text" name="height" id="height" runat="server" style="visibility:hidden;"/>   
    </div>

 

 

 

 

 

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,如需转载请自行联系原作者

相关文章
|
移动开发 前端开发 JavaScript
【移动端】实现相册的上传和缩放裁剪
做项目时,在移动端,需要实现用户相册图片的上传,并对图片进行缩放裁剪的功能。下面说一下实现流程。
160 1
【移动端】实现相册的上传和缩放裁剪
|
数据安全/隐私保护 Android开发
Android为图片添加水印,裁剪图片,旋转图片工具类
Android为图片添加水印,裁剪图片,旋转图片工具类
142 0
|
XML 存储 数据安全/隐私保护
Android自定义控件(九)——如何让图片颜色更鲜艳以及给图片添加水印
Android自定义控件(九)——如何让图片颜色更鲜艳以及给图片添加水印
214 0
Android自定义控件(九)——如何让图片颜色更鲜艳以及给图片添加水印
裁剪图片
裁剪图片
59 0
裁剪图片
|
Python
图片旋转 90、180、270
图片旋转 90、180、270
150 0
|
C# 小程序
给图片加上阴影效果
原文:给图片加上阴影效果 今天写一个小程序有一个给图片加上阴影的需求,记得WPF的Effect中就有阴影特效,就打算用它了。代码如下:     using (var imageStreamSource = File.
1174 0
|
C# 图形学 索引
上传图片时,使用GDI+中重绘方式将CMYK图片转为RGB图片
原文:上传图片时,使用GDI+中重绘方式将CMYK图片转为RGB图片 我们知道,如果网站上传图片时,如果用户上传的是CMYK图片,那么在网站上将是无法显示的,通常的现象是出现一个红叉。
1165 0