Pickels,
In the 1.2, we will include image resize API function for binary resource. Unfortunately, 1.2 is not public released yet.
In your current situation, I suggest doing the following.
1. put your image resize code into a content template. put the image path as query string as you have now, thumbnail width, height and other variables as predefined value or also as query string.
2. Create a page called "resize", and add above content template into this page. You can also do it with pageplugin, but most of people like content template better than pageplugin.
3. In the place where you needed resized thumbnail image, change the url into looks like <img src="/resize?imagepath=[encoded_file_path]" border="0" />. You can generate this resize page link using PageUrl function to make it more portable.
This can be a solution for now. I did not see the "getSize" function, so I do not know how you calculate the thumbnail width and height. I post some of our image resizing code here in case other people might need it as well.
Resizing is very common in .NET, so your code are really fine as well.
resize
Code:
// create an image and calculate the width/height.
System.Drawing.Image img = System.Drawing.Image.FromFile(imageFullPath);
int width = ThumbnailWidth;
int height = Convert.ToInt32((double)ThumbnailWidth / (double)img.Width * (double)img.Height);
if (height > ThumbnailHeight)
{
height = ThumbnailHeight;
width = Convert.ToInt32((double)ThumbnailHeight / (double)img.Height * (double)img.Width);
}
context.Response.ContentType = "image/png";
Bitmap Target = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(Target))
{
graphics.CompositingQuality = CompositingQuality.HighSpeed;
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.InterpolationMode = InterpolationMode.High;
graphics.DrawImage(img, 0, 0, width, height);
// Target = this.ConvertToGrayscale(Target);
using (MemoryStream memoryStream = new MemoryStream())
{
//if (quality == "low")
//{
// Target.Save(memoryStream, ImageFormat.Gif);
//}
//else
//{
Target.Save(memoryStream, ImageFormat.Jpeg);
//}
memoryStream.WriteTo(HttpContext.Current.Response.OutputStream);
}
}
Also sample code to make an image to Grayscale.
Code:
public Bitmap ConvertToGrayscale(Bitmap source)
{
Bitmap bm = new Bitmap(source.Width, source.Height);
for (int y = 0; y < bm.Height; y++)
{
for (int x = 0; x < bm.Width; x++)
{
Color c = source.GetPixel(x, y);
int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
bm.SetPixel(x, y, Color.FromArgb(luma, luma, luma));
}
}
return bm;
}
Regards,
Vincent
Kooboo Team