图片裁剪PhotoCropper
;
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, Im |
|
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn
为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系TTP/1.1 401 Access Denied
|
Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved
地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008
电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134
《中华人民共和国增值电信业务经营许可证》闽B2-20100024 ICP备案:闽ICP备05037997号