C# 裁剪图像

C# 裁剪图像,c#,asp.net,C#,Asp.net,我有一个要求,即图像被裁剪并保存在数据库中。 当用户上传他/她的照片时,首先由该原始图像生成一个大尺寸图像。 然后裁剪大图像并保存为拇指图像。 所以有3种尺寸:原始尺寸、大尺寸(需要用户裁剪)、拇指图像 所以我在互联网上找到了一个控件,可以进行这种裁剪。 这是用于制作原始图像的大型图像的代码 public static byte[] MakeThumb(byte[] fullsize, int maxwidth) { Image iOriginal, iThumb;

我有一个要求,即图像被裁剪并保存在数据库中。 当用户上传他/她的照片时,首先由该原始图像生成一个大尺寸图像。 然后裁剪大图像并保存为拇指图像。 所以有3种尺寸:原始尺寸、大尺寸(需要用户裁剪)、拇指图像

所以我在互联网上找到了一个控件,可以进行这种裁剪。 这是用于制作原始图像的大型图像的代码

public static byte[] MakeThumb(byte[] fullsize, int maxwidth)
    {
        Image iOriginal, iThumb;
        double scale;

        iOriginal = Image.FromStream(new MemoryStream(fullsize));
        if (iOriginal.Width > maxwidth)
        {
            scale = iOriginal.Width / maxwidth;
            int newheight = Convert.ToInt32(iOriginal.Height / scale);
            iThumb = new Bitmap(iOriginal, maxwidth, newheight);
            MemoryStream m = new MemoryStream();
            iThumb.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
            return m.GetBuffer();
        }
        else
        {
            return fullsize;
        }
    }
生成大图像后,需要对其进行裁剪。这是我用来裁剪图像的代码/dll

<asp:Button ID="btnCrop" runat="server" Text="Crop" onclick="btnCrop_Click" />

 <cs:CropImage ID="wci1" runat="server" 
            Image="Image1"            
            MinSize="100,100"
            MaxSize="150,150"
            W="150"
            H="150"
             />      

protected void btnCrop_Click(object sender, EventArgs e)
    {

        /* thats the method to crop and save the image.*/
        wci1.Crop(Server.MapPath("images/cropped.jpg"));

        /*
         this part is just to show the image after its been cropped and saved! so focus on just the code above.
         */
        Image img = new Image();
        img.ImageUrl = "images/cropped.jpg?rnd=" + (new Random()).Next(); // added random for caching issue.
        this.Controls.Add(img);
    }

受保护的无效btnCrop_单击(对象发送者,事件参数e)
{
/*这是裁剪和保存图像的方法*/
wci1.Crop(Server.MapPath(“images/croped.jpg”);
/*
这部分只是在图像被裁剪和保存后显示图像!所以只关注上面的代码。
*/
图像img=新图像();
img.ImageUrl=“images/crapped.jpg?rnd=“+(new Random()).Next();//为缓存问题添加了随机项。
this.Controls.Add(img);
}
Crop(Server.MapPath(“images/croped.jpg”))将images/croped.jpg作为其参数。 我应该如何修改代码,使其采用大图像而不是“images/cropped.jpg” 我的意思是:wci1.Crop(Server.MapPath(largeimage)

先谢谢你
Sun

将大版本保存到磁盘,然后在方法中引用该版本

然后你就可以使用

File.IO.Delete

编写一个包装器方法并调用它,而不是wci1.Crop
。在包装器方法内部,您可以调用
wci1.Crop`