ASI-fujibikes
-
11/18/2009 1:40:29 PM
RE:Resize image function issue
Below is the resize image code, Let me know if you find anything
using System; using System.Data; using System.Configuration; using System.Collections; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Text.RegularExpressions; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using CMS.CMSHelper; using CMS.FileManager; using CMS.GlobalHelper; using CMS.TreeEngine;
public partial class CMSTemplates_ASIASPX_ResizeImage : Page { private int width; private int height; private bool? backgroundImage;
public int Width { get { if (width == 0 && !string.IsNullOrEmpty(Request["w"])) width = Convert.ToInt32(Request["w"]); return width; } set { width = value; } }
public int Height { get { if (height == 0 && !string.IsNullOrEmpty(Request["h"])) height = Convert.ToInt32(Request["h"]); return height; } set { height = value; } }
public bool? BackgroundImage { get { if (!string.IsNullOrEmpty(Request["b"])) backgroundImage = Request["b"] == "true"; else backgroundImage = false; return backgroundImage;
} }
protected void Page_Load(object sender, EventArgs e) { try { // string imagePath = Request["imagepath"]; int nodeId = Int32.Parse(Request["nodeID"]); TreeProvider tree = new TreeProvider();
TreeNode node = tree.SelectSingleNode(nodeId);//tree.SelectSingleNode(guid,CMSContext.PreferredCultureCode, CMSContext.CurrentSiteName); if (node != null) { AttachmentManager am = new AttachmentManager(tree.Connection); Guid existingGuid = ValidationHelper.GetGuid(node.GetValue("FileAttachment"), Guid.Empty); if (existingGuid == Guid.Empty) { existingGuid = ValidationHelper.GetGuid(node.GetValue("DetailPhoto"), Guid.Empty); } if (existingGuid != Guid.Empty) { AttachmentInfo ai = am.GetAttachmentInfo(existingGuid, CMSContext.CurrentSiteName); write(ai.AttachmentBinary, ai.AttachmentMimeType); }
} } catch (Exception ex) { string path = string.Format("~/App_Themes/{0}/img/noDetail.png", "Fuji"); if (File.Exists(Server.MapPath(path))) Response.Redirect(Server.MapPath(path)); else string.Format("~/App_Themes/{0}/img/noDetail.png", "Fuji"); } }
private void write(byte[] image, string mimeType) { ResizeStream(ref image); Response.ContentType = mimeType; Response.BinaryWrite(image); }
private string currentTheme() { TreeNode homeNode = TreeHelper.SelectSingleNode("/Home", true, "ASI.HomePage"); string sitetheme = homeNode.GetValue("SiteThemeName").ToString().Trim() ?? "Fuji"; return sitetheme; }
private void ResizeStream(ref byte[] image) { using (WebImage orig = new WebImage(Image.FromStream(new MemoryStream(image)))) { Color bgColor = Color.FromArgb(0, Color.White);
if (Height == 0) { float dheight = ((orig.Height / (float)orig.Width) * Width); Height = (int)dheight; }
SizeF imageSize = new SizeF(orig.Width, orig.Height); SizeF resizeSize = new SizeF(Width, Height); if (BackgroundImage.Value) resizeSize = new SizeF(Width - 5, Height - 5);
SizeF newSize = WebImage.CalcNewSize(imageSize, resizeSize);
using (WebImage newImage = new WebImage(bgColor, Width, Height)) { int nodeId = Int32.Parse(Request["nodeID"]);
if (BackgroundImage.Value) { string path = string.Format("~/App_Themes/{0}/img/BG-thumb.png", currentTheme()); if (File.Exists(Server.MapPath(ResolveUrl(path)))) newImage.AddImage(Server.MapPath(ResolveUrl(path)), 0, 0); orig.ResizeExact((int)newSize.Width, (int)newSize.Height, Color.White); newImage.AddImage(orig.Image, (int)((Width - (int)newSize.Width) / 2), (int)((Height - (int)newSize.Height) / 2)); } else { newImage.AddImage(orig.Image, 0, 0, (int)newSize.Width, (int)newSize.Height); } //IE 6.0 hack if (Request.Browser.Browser == "IE" && Convert.ToDouble(Request.Browser.Version) < 7.0) newImage.ConvertToIndexedColor(bgColor, new OctreePalletizer(255, 8)); image = newImage.ToBytes(ImageFormat.Png);//orig.ToBytes(ImageFormat.Gif); }
} }
private bool isTrans(Color srcPixel) { if (srcPixel.A < 128) return true; return false; }
}
|