Bug reports Found a bug? Post it here please.
Version 4.x > Bug reports > Resize image function issue View modes: 
User avatar
Member
Member
ASI-fujibikes - 11/4/2009 11:43:17 AM
   
Resize image function issue
Hi,

I have a custom function in the website Resize image, its been there since kentico ... I had done my upgrade to 4.1.3 and found some issues but was able to resolve it from my side itself... but recently i did the upgrade to 4.1.9 and then this caused a problem in my resize function it does not work properly...

basically what this function does is gets the image attachment usig the nodeid parameter, and in the transformation we pass the 3 parameters width, height and background(bool) ..so it gets the image resizes and if background is true then it replaces the background with custom image in the themes... but this is not working properly nowafter the upgrade... I still have the 4.1.3 version on my staging and it works fine there.. I was wondering if there was any changes during upgrade done that might be an issue here...

kindly any response is appreciated..

Thanks,

Sid

User avatar
Member
Member
kentico_pavelk - 11/10/2009 7:53:53 AM
   
RE:Resize image function issue
Hi Sid,

Could you please post the code of your custom function here? I will inspect it.


Best regards,
Pavel Knotek

User avatar
Member
Member
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;
}


}

User avatar
Member
Member
kentico_pavelk - 12/8/2009 3:52:00 AM
   
RE:Resize image function issue
Hello,

Could you please describe your problem in more details? What exactly doesn't work properly? Could you please also try to debug the page and see where the problem might be?


Best regards,
Pavel Knotek

User avatar
Member
Member
mostmore434-yahoo - 8/28/2013 11:29:08 PM
   
RE:Resize image function issue
yes, i can't figure out your problem if the details are not specific. here is some image resizing codes you may wanna try. it is c# from a very simple but powerful image tool. i have been using this opem source for a long time. hope it is useful.