裁剪图像
问题描述:
我有一个需要将图像裁剪并保存在数据库中。 当用户上传他/她的照片时,首先使用该原始图像制作大尺寸图像。 然后将大图像裁剪并保存为拇指图像。 所以有3种尺寸:原始,大(需要由用户剪裁),拇指图像。裁剪图像
所以我在互联网上找到了一个控制这个剪裁。 http://webcropimage.codeplex.com/releases/view 这是用于使原厂
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;
}
}
一旦生成大的图像,它需要被修剪的大的图像的代码。下面是我使用的裁剪图像
<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);
}
wci1.Crop(使用Server.Mappath( “图像/ cropped.jpg”))代码/ DLL拍摄图像/ cropped.jpg作为它的参数。 我应该如何改变代码,以便它需要一个大的图像,而不是“图像/ cropped.jpg” 我的意思是这样:wci1.Crop(使用Server.Mappath(largeimage)
预先感谢您 孙
答
保存大版到磁盘,然后引用的方法。
然后,您可以只使用
File.IO.Delete
事后将其删除。
编写一个包装方法,并调用它来代替wci1.Crop'。并且在包装方法中,你可以调用'wci1.Crop' – 2012-01-06 06:24:34
谢谢@RanhiruCooray – user575219 2012-01-08 17:53:57