private string getFilePath(string subdir, WebImageFormat format) { string dir = Path.Combine(workingDirectory, subdir); string fp = Path.Combine(dir, serverID); fp = appendExtension(fp, format); if (!File.Exists(fp)) { return fp; } else { // a file already exists with this name. This will happen when we're // reusing a raw file. // need to save it with a versionnumber appended, e.g. test(2).gif int version = 2; int extPos = fp.LastIndexOf("."); String preext = fp.Substring(0, extPos); String ext = fp.Substring(extPos); fp = preext + "(" + version + ")" + ext; while (File.Exists(fp)) { fp = preext + "(" + (++version) + ")" + ext; } return fp; } }
protected override void Render(HtmlTextWriter writer) { if (this.Page == null || this.Page.Site == null || !this.Page.Site.DesignMode) { StringBuilder sb = new StringBuilder(); sb.Append(ResolveUrl(HandlerUrl)); sb.Append("?url="); sb.Append(ResolveUrl(ImageUrl)); if (!IsNullOrEmpty(BackgroundImageUrl)) { sb.Append("&bg="); sb.Append(ResolveUrl(BackgroundImageUrl)); } if (!IsNullOrEmpty(ForegroundImageUrl)) { sb.Append("&fg="); sb.Append(ResolveUrl(ForegroundImageUrl)); } if (!IsNullOrEmpty(NotFoundImageUrl)) { sb.Append("&nf="); sb.Append(ResolveUrl(NotFoundImageUrl)); } if (DrawSize.Height > 0) { sb.Append("&h="); sb.Append(DrawSize.Height); } if (DrawSize.Width > 0) { sb.Append("&w="); sb.Append(DrawSize.Width); } if (VerticalAlignment != VerticalAlignment.NotSet) { sb.Append("&y="); sb.Append(VerticalAlignment.ToString().Substring(0, 1).ToLower()); } else if (DrawTop > 0) { sb.Append("&y="); sb.Append(DrawTop); } if (HorizontalAlignment != HorizontalAlignment.NotSet) { sb.Append("&x="); sb.Append(HorizontalAlignment.ToString().Substring(0, 1).ToLower()); } else if (DrawLeft > 0) { sb.Append("&x="); sb.Append(DrawLeft); } if (NoCache) { sb.Append("&nocache="); } if (Cache > 0) { sb.Append("&cache="); sb.Append(Cache); } if (SmoothingMode != SmoothingMode.Default) { sb.Append("&sm="); sb.Append(SmoothingMode); } if (CompositingQuality != CompositingQuality.Default) { sb.Append("&cq="); sb.Append(CompositingQuality); } if (WebImageFormat != WebImageFormat.Auto) { sb.Append("&f="); sb.Append(WebImageFormat.ToString()); } ImageUrl = sb.ToString(); } base.Render(writer); }
protected override void LoadControlState(object savedState) { string[] state = (string[])savedState; if (!Int32.TryParse(state[0], out intImageWidth)) intImageWidth = -1; if (!Int32.TryParse(state[1], out intImageHeight)) intImageHeight = -1; serverImgID = state[2]; originalSrc = state[3]; controlMode = (ControlMode)Enum.Parse(typeof(ControlMode), state[4]); hasChanged = (state[5] == "1"); if (!Int32.TryParse(state[6], out canvasWidth)) canvasWidth = 0; if (!Int32.TryParse(state[7], out canvasHeight)) canvasHeight = 0; workingDirectory = state[8]; webImageFormat = (WebImageFormat)Enum.Parse(typeof(WebImageFormat), state[9]); webImageQuality = (WebImageQuality)Enum.Parse(typeof(WebImageQuality), state[10]); if (!Int32.TryParse(state[11], out rawWidth)) rawWidth = 0; if (!Int32.TryParse(state[12], out rawHeight)) rawHeight = 0; canvasImageName = state[13]; webImageName = state[14]; handlerPath = state[15]; if (!Single.TryParse(state[16], out aspectRatio)) aspectRatio = 0; if (!Int32.TryParse(state[17], out thumbnailSize)) thumbnailSize = 64; }
private string appendExtension(string path, WebImageFormat format) { switch (format) { case WebImageFormat.Gif: path += ".gif"; break; case WebImageFormat.Jpg: path += ".jpg"; break; case WebImageFormat.Png: path += ".png"; break; } return path; }
public string SaveRawImageAsWebImage(WebImageFormat format, WebImageQuality quality) { string filePath = getFilePath(WebImageMaker.WebImageDirName, format); using (Image img = Image.FromFile(getRawFilePath())) { // if the raw image is the same format then just copy the raw image: if (getGDIFormat(format).Equals(img.RawFormat)) { File.Copy(getRawFilePath(), filePath); } else { // we need to do some conversion, but don't bother with quality settings - // if it's the wrong format then the quality isn't going to be great anyway img.Save(filePath, getGDIFormat(format)); } } return Path.GetFileName(filePath); }
public string CropAndScale(System.Drawing.Rectangle transformedSelection, WebImageFormat format, WebImageQuality quality, int reqdWidth, int reqdHeight) { if (reqdWidth < 1) { reqdWidth = 100; } if (reqdHeight < 1) { reqdHeight = 100; } string webFileName = null; Rectangle dest = new Rectangle(0, 0, reqdWidth, reqdHeight); using (Image rawImg = Image.FromFile(getRawFilePath())) { using (Bitmap webImage = new Bitmap(reqdWidth, reqdHeight, PixelFormat.Format24bppRgb)) { using (Graphics g = Graphics.FromImage(webImage)) { setGraphicsQuality(g, quality); g.DrawImage(rawImg, dest, transformedSelection, GraphicsUnit.Pixel); string filePath = getFilePath(WebImageMaker.WebImageDirName, format); webImage.Save(filePath, getGDIFormat(format)); webFileName = Path.GetFileName(filePath); } } } return webFileName; }
public string CreateCanvas(WebImageFormat format, WebImageQuality quality, int canvasWidth, int canvasHeight) { purge(WebImageMaker.CanvasImageDirName); string canvasFileName = null; using (Image rawImg = Image.FromFile(getRawFilePath())) { using (Bitmap canvas = new Bitmap(canvasWidth, canvasHeight, PixelFormat.Format24bppRgb)) { using (Graphics g = Graphics.FromImage(canvas)) { setGraphicsQuality(g, quality); g.DrawImage(rawImg, 0, 0, canvasWidth, canvasHeight); string filePath = getFilePath(WebImageMaker.CanvasImageDirName, format); canvas.Save(filePath, getGDIFormat(format)); canvasFileName = Path.GetFileName(filePath); } } } return canvasFileName; }
private string CreateThumbnail(Image img, WebImageFormat format) { string thumbFileName = null; Rectangle rawRect = new Rectangle(0, 0, img.Width, img.Height); Rectangle thumbRect = new Rectangle(); float fWidth = (float)img.Width; float fHeight = (float)img.Height; float fThumbSize = (float)thumbnailSize; float aspectRatio = fWidth / fHeight; if (aspectRatio > 1) { thumbRect.Width = thumbnailSize; thumbRect.X = 0; thumbRect.Height = Convert.ToInt32((fThumbSize / fWidth) * fHeight); thumbRect.Y = (thumbnailSize - thumbRect.Height) / 2; } else { thumbRect.Height = thumbnailSize; thumbRect.Y = 0; thumbRect.Width = Convert.ToInt32((fThumbSize / fHeight) * fWidth); thumbRect.X = (thumbnailSize - thumbRect.Width) / 2; } using (Bitmap thumb = new Bitmap(thumbnailSize, thumbnailSize, PixelFormat.Format24bppRgb)) { using (Graphics g = Graphics.FromImage(thumb)) { setGraphicsQuality(g, WebImageQuality.High); g.Clear(Color.White); g.DrawImage(img, thumbRect, rawRect, GraphicsUnit.Pixel); string filepath = getFilePath(WebImageMaker.ThumbnailImageDirName, format); thumb.Save(filepath, getGDIFormat(format)); thumbFileName = Path.GetFileName(filepath); } } return thumbFileName; }
private ImageFormat getGDIFormat(WebImageFormat format) { ImageFormat fmt = null; switch (format) { case WebImageFormat.Gif: fmt = ImageFormat.Gif; break; case WebImageFormat.Jpg: fmt = ImageFormat.Jpeg; break; case WebImageFormat.Png: fmt = ImageFormat.Png; break; } return fmt; }