/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public virtual void OnCropping(Object sender, CropImageEventArgs e) { if (Cropping != null) { Cropping(this, e); } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public virtual void OnCropped(Object sender, CropImageEventArgs e) { //EnsureChildControls(); if (Cropped != null) { Cropped(this, e); } }
/// <summary> /// Call this function when button clicked. /// </summary> /// <param name="path"></param> public void Crop(string path) { //EnsureChildControls(); CropImageEventArgs args = new CropImageEventArgs(); args.X = Convert.ToInt32(Page.Request["_x"]); args.Y = Convert.ToInt32(Page.Request["_y"]); args.Width = Convert.ToInt32(Page.Request["_w"]); args.Height = Convert.ToInt32(Page.Request["_h"]); this.X = args.X; this.Y = args.Y; this.W = args.Width; this.H = args.Height; this.X2 = args.Width + args.X; this.Y2 = args.Height + args.Y; //repair size because of canvas fitting double fact = CanvasRatio; if (fact > 0) { args.X = Convert.ToInt32(args.X / fact); args.Y = Convert.ToInt32(args.Y / fact); args.Width = Convert.ToInt32(args.Width / fact); args.Height = Convert.ToInt32(args.Height / fact); } // maintain details after postback! //HttpContext.Current.Response.Write("w:" + this.W); OnCropping(this, args); // create property called save as and save this file to there. System.Drawing.Graphics g = null; System.Drawing.Bitmap cropedImage = null; drawing.Image image = null; try { image = drawing.Image.FromStream(new MemoryStream(System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath(this.ImagePath)))); } catch (Exception ex) { Stream stream = null; try { WebRequest req = WebRequest.Create(this.ImagePath); WebResponse response = req.GetResponse(); stream = response.GetResponseStream(); image = drawing.Image.FromStream(stream); stream.Close(); } catch (Exception ex2) { stream.Close(); throw ex2; } } if (EnablePreview) { cropedImage = new Bitmap( this.PreviewWidth, this.PreviewHeight , image.PixelFormat); } else { cropedImage = new Bitmap( args.Width , args.Height , image.PixelFormat); } g = System.Drawing.Graphics.FromImage(cropedImage); drawing.Rectangle _rec; drawing.Rectangle _rec2; if (EnablePreview) { _rec = new drawing.Rectangle( 0, 0, this.PreviewWidth, this.PreviewHeight); _rec2 = new drawing.Rectangle( args.X, args.Y, args.Width, args.Height); } else { _rec = new drawing.Rectangle(0, 0, args.Width, args.Height); _rec2 = new drawing.Rectangle( args.X, args.Y, args.Width, args.Height); } g.DrawImage(image, _rec, _rec2.X, _rec2.Y , _rec2.Width, _rec2.Height , drawing.GraphicsUnit.Pixel); string mimeType = GetMimeType(image); if (mimeType == "image/unknown") { cropedImage.Save(path); } else if (mimeType == "image/jpeg") { cropedImage.Save(path, ImageFormat.Jpeg); } else if (mimeType == "image/gif") { cropedImage.Save(path, ImageFormat.Gif); } else if (mimeType == "image/x-png") { cropedImage.Save(path, ImageFormat.Png); } else if (mimeType == "image/x-ms-bmp") { cropedImage.Save(path, ImageFormat.Bmp); } image.Dispose(); g.Dispose(); cropedImage.Dispose(); OnCropped(this, args); }
public void CropActualPic(int compressedHeight, string cropImgPath) { drawing.Image image = null; int actualHeight = 0; try { image = drawing.Image.FromStream(new MemoryStream(System.IO.File.ReadAllBytes(this.ImagePhysicalPath))); actualHeight = image.Height; } catch (Exception ex) { Stream stream = null; try { WebRequest req = WebRequest.Create(this.ImagePath); WebResponse response = req.GetResponse(); stream = response.GetResponseStream(); image = drawing.Image.FromStream(stream); actualHeight = image.Height; stream.Close(); } catch (Exception ex2) { stream.Close(); throw ex2; } } float aspectRatio = (float)actualHeight / (float)compressedHeight; CropImageEventArgs args = new CropImageEventArgs(); args.X = (int)(Convert.ToInt32(Page.Request["_x"]) * aspectRatio); args.Y = (int)(Convert.ToInt32(Page.Request["_y"]) * aspectRatio); args.Width = (int)(Convert.ToInt32(Page.Request["_w"]) * aspectRatio); args.Height = (int)(Convert.ToInt32(Page.Request["_h"]) * aspectRatio); this.X = args.X; this.Y = args.Y; this.W = args.Width; this.H = args.Height; this.X2 = args.Width + args.X; this.Y2 = args.Height + args.Y; Bitmap cropedImage = new Bitmap( args.Width , args.Height , PixelFormat.Format24bppRgb); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(cropedImage); drawing.Rectangle _rec; drawing.Rectangle _rec2; _rec = new drawing.Rectangle(0, 0, args.Width, args.Height); _rec2 = new drawing.Rectangle( args.X, args.Y, args.Width, args.Height); g.DrawImage(image, _rec, _rec2.X, _rec2.Y , _rec2.Width, _rec2.Height , drawing.GraphicsUnit.Pixel); string mimeType = GetMimeType(image); if (mimeType == "image/unknown") { cropedImage.Save(cropImgPath); } else if (mimeType == "image/jpeg") { cropedImage.Save(cropImgPath, ImageFormat.Jpeg); } else if (mimeType == "image/gif") { cropedImage.Save(cropImgPath, ImageFormat.Gif); } else if (mimeType == "image/x-png" || mimeType == "image/png") { cropedImage.Save(cropImgPath, ImageFormat.Png); } else if (mimeType == "image/x-ms-bmp") { cropedImage.Save(cropImgPath, ImageFormat.Bmp); } if (image != null) { image.Dispose(); } g.Dispose(); cropedImage.Dispose(); OnCropped(this, args); }
private Stream CropActualPic(int compressedSize, bool isHeight) { drawing.Image image = null; Stream returnStream = new MemoryStream(); int actualSize = 0; try { if (this.ImagePhysicalPath != string.Empty) { image = drawing.Image.FromStream(new MemoryStream(System.IO.File.ReadAllBytes(this.ImagePhysicalPath))); } else { image = drawing.Image.FromStream( new MemoryStream( System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath(this.ImagePath)))); } } catch (Exception ex) { Stream stream = null; try { WebRequest req = WebRequest.Create(this.ImagePath); WebResponse response = req.GetResponse(); stream = response.GetResponseStream(); image = drawing.Image.FromStream(stream); stream.Close(); } catch (Exception ex2) { stream.Close(); throw ex2; } } actualSize = (isHeight) ? image.Height : image.Width; float aspectRatio = (float)actualSize / (float)compressedSize; CropImageEventArgs args = new CropImageEventArgs(); args.X = (int)(Convert.ToInt32(Page.Request["_x"]) * aspectRatio); args.Y = (int)(Convert.ToInt32(Page.Request["_y"]) * aspectRatio); args.Width = (int)(Convert.ToInt32(Page.Request["_w"]) * aspectRatio); args.Height = (int)(Convert.ToInt32(Page.Request["_h"]) * aspectRatio); this.X = args.X; this.Y = args.Y; this.W = args.Width; this.H = args.Height; this.X2 = args.Width + args.X; this.Y2 = args.Height + args.Y; Bitmap cropedImage = new Bitmap( args.Width , args.Height , image.PixelFormat); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(cropedImage); drawing.Rectangle _rec; drawing.Rectangle _rec2; _rec = new drawing.Rectangle(0, 0, args.Width, args.Height); _rec2 = new drawing.Rectangle( args.X, args.Y, args.Width, args.Height); g.DrawImage(image, _rec, _rec2.X, _rec2.Y , _rec2.Width, _rec2.Height , drawing.GraphicsUnit.Pixel); string mimeType = GetMimeType(image); if (mimeType == "image/unknown") { cropedImage.Save(returnStream, ImageFormat.Jpeg); } else if (mimeType == "image/jpeg") { cropedImage.Save(returnStream, ImageFormat.Jpeg); } else if (mimeType == "image/gif") { cropedImage.Save(returnStream, ImageFormat.Gif); } else if (mimeType == "image/x-png") { cropedImage.Save(returnStream, ImageFormat.Png); } else if (mimeType == "image/x-ms-bmp") { cropedImage.Save(returnStream, ImageFormat.Bmp); } image.Dispose(); g.Dispose(); cropedImage.Dispose(); OnCropped(this, args); return(returnStream); }