Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Image Resize
Pickels
#1 Posted : Monday, September 28, 2009 10:25:35 PM(UTC)
Rank: Advanced Member
Groups: Registered

Joined: 9/8/2009(UTC)
Posts: 91
Location: Belgium
Hello,
I was wondering where I should put the logic to image resizing. In asp.net webforms I just would make an extra page that returns the resized bitmap. In Mvc I guess I would let an action return the resized image. Not sure where to do it in kooboo.
jifeng
#2 Posted : Tuesday, September 29, 2009 8:52:46 AM(UTC)
Rank: Administration
Groups: Administrators, Registered

Joined: 9/3/2009(UTC)
Posts: 1,555
Location: Xiamen China
Hi Pickels:
I'm wonder what logic you want to resize image. Can you put some sample code? If you want to resized image at front page. I think you may write some server code at content template, but I'm not very sure what logic you want to write.
Regards,

Jifeng Huang

Kooboo Team

Microsoft ASP.NET MVP
Pickels
#3 Posted : Tuesday, September 29, 2009 5:24:54 PM(UTC)
Rank: Advanced Member
Groups: Registered

Joined: 9/8/2009(UTC)
Posts: 91
Location: Belgium
This is the code I mostly use for thumbnail images.

Code:

protected void Page_Load(object sender, EventArgs e)
{
if (queryStringCheck("path"))
{
string path = Request.QueryString["path"];
Bitmap bmp = new Bitmap(Server.MapPath(path));
MemoryStream ms = new MemoryStream();
Response.ContentType = "image/png";
Response.Clear();

Response.BufferOutput = true;

int width;
int height;

if (queryStringCheck("w") || queryStringCheck("h"))
{
width = getSize("w", bmp);
height = getSize("h", bmp);

bmp = new Bitmap(bmp, width, height);
bmp.Save(ms , System.Drawing.Imaging.ImageFormat.Png);

ms.WriteTo(Response.OutputStream);

bmp.Dispose();
                Response.End();

ms.Dispose();
ms.Close();
}
}
}
zguoqi
#4 Posted : Tuesday, September 29, 2009 5:48:27 PM(UTC)
Rank: Administration
Groups: Registered, Administrators

Joined: 9/2/2009(UTC)
Posts: 695
Location: xiamen
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
Pickels
#5 Posted : Tuesday, September 29, 2009 10:22:29 PM(UTC)
Rank: Advanced Member
Groups: Registered

Joined: 9/8/2009(UTC)
Posts: 91
Location: Belgium
Thanks for the reply I was thinking of doing something like you suggested but I wanted to make sure if it was a good idea to do it that way.
pgeza1
#6 Posted : Wednesday, October 26, 2011 9:21:50 AM(UTC)
Rank: Newbie
Groups: Registered

Joined: 10/25/2011(UTC)
Posts: 9
Location: Budapest
Hi all!

Is something happened with this issue?
Is there a built in way? I think it would make sense to store the genereated image somewhere on server for use in other requests. Is it possible?

Thanks,
Geza
Hrvoje_86
#7 Posted : Friday, October 28, 2011 8:49:19 PM(UTC)
Rank: Advanced Member
Groups: Registered

Joined: 1/28/2011(UTC)
Posts: 157
Location: Croatia
Yes resizing images is very easy now.
And kooboo caches the image and uses the cached version later automatically.

Code:

@Url.FrontUrl().ResizeImageUrl(imagePath, 640,480,true,90)
1 user thanked Hrvoje_86 for this useful post.
jifeng on 11/1/2011(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF 1.9.5.5 | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.123 seconds.