Thumbnails in Blogengine.NET

by James Archuleta

August/12/2009 23:36

After searching the web and being unable to find one that works. I hacked up my own.

I had to change the Add Entry pages, as I was unable to make this happen using the extensions to blogengine.net, ideally there should be a better way to do this, but I was unable to find one.

Here's how I did it.

Add_entry.aspx (7.31 kb)


<asp:CheckBox ID="CheckBoxThumb" runat="server" Text="Generate Thumbnail" Checked="true" />

 

 

Add_entry.aspx.cs (14.10 kb)

if (CheckBoxThumb.Checked)
{
    string thumbPath = Server.MapPath(folder + relativeFolder);
    int width = 250; // you can change the size here
    GenerateThumbnail(thumbPath, fileName, width);
    img = string.Format("<a class="\"lightbox\"" 
        href="\"{0}image.axd?picture={1}\""> 
        <img src="\"{0}image.axd?picture={2}\"" alt="\"\" />", path, 
        Server.UrlEncode(relativeFolder.Replace("\\", "/"") + fileName),    
        Server.UrlEncode(relativeFolder.Replace("\\", "/"") + "thumb_"  
        + fileName));

    txtContent.Text += img;
    txtRawContent.Text += img;
}
else
{
    img = string.Format("&ltimg src="\"{0}image.axd?picture={1}\""
       alt="\"\"" />",
path, Server.UrlEncode(relativeFolder.Replace("
\\", "/") + fileName)); txtContent.Text += img; txtRawContent.Text += img; }
private void GenerateThumbnail(string path, string filename, int width)
{
    using (Image orig = Image.FromFile(path + filename))
    {
        this.GenerateThumbnail(orig, new Size(width, 
               CalculateHeight(orig, width)), 
               GetFormat(filename), path + "thumb_" + filename);            
    }
}

private void GenerateThumbnail( System.Drawing.Image orig, 
                Size size,ImageFormat format, 
                String location)
{
        System.Drawing.Image.GetThumbnailImageAbort callback = new 
System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); System.Drawing.Image img = orig.GetThumbnailImage(size.Width, size.Height, callback, IntPtr.Zero); img.Save(location, format); } private static ImageFormat GetFormat(string filename) { if (filename.EndsWith("jpg") || filename.EndsWith("jpeg") || filename.EndsWith("tiff")) return ImageFormat.Jpeg; return ImageFormat.Png; } private static int CalculateHeight( System.Drawing.Image img, double desiredWidth) { double power = img.Width / desiredWidth; return (int)(img.Height / power); } private bool ThumbnailCallback() { return false; }

The whole thing came out really nice. I also added the Jquery Lightbox plugin. for the images.

Comments

eric mooney Ireland 8/20/2009 5:22:46 AM

eric mooney

hey james,

thanks for the codes.  got it working a treat!
i was wondering, could you modify the
pages files so one could do the same? i am hoping
to put in a gallery of sorts on a sub page

cheers!
Eric

Reply
Travel Agent Indonesia United States 9/9/2009 11:42:11 AM

Travel Agent Indonesia

Great share and nice website thanks

Reply
reborn baby United States 9/14/2009 5:29:21 AM

reborn baby

Thanks for this...

Reply
joomla website templates United States 9/14/2009 8:25:54 AM

joomla website templates

I admit, I have not been on this webpage in a long time... however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues.

Reply
Travel Agents Indonesia United States 9/15/2009 3:50:21 PM

Travel Agents Indonesia

Nice Share and Great Website ..Many Thanks

Reply
Free Blackberry Applications United States 9/20/2009 11:32:50 AM

Free Blackberry Applications

great post..

Reply
Women Seeking Men United States 9/29/2009 8:44:50 AM

Women Seeking Men

I admire what you have done here. I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well.

Reply
resume building United States 9/30/2009 5:43:12 AM

resume building

It resulted in a very nice outcome, nice job around here.. maybe I'll try to experiment on my site also.

Reply

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading