473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Image thumbnails

16
I have done some reading on this and my thumb nail generation works great with a few exceptions, this is what I need help with.

My code:

Expand|Select|Wrap|Line Numbers
  1.         public static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
  2.         {
  3.             using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
  4.             {
  5.                 Size newSize = CalculateDimensions(oldImage.Size, targetSize);
  6.  
  7.                 using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
  8.                 {
  9.                     using (Graphics canvas = Graphics.FromImage(newImage))
  10.                     {
  11.                         canvas.SmoothingMode = SmoothingMode.AntiAlias;
  12.                         canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
  13.                         canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
  14.                         canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));
  15.                         MemoryStream m = new MemoryStream();
  16.                         newImage.Save(m, ImageFormat.Jpeg);
  17.                         return m.GetBuffer();
  18.                     }
  19.                 }
  20.  
  21.             }
  22.         }
  23.  
  24.         private static Size CalculateDimensions(Size oldSize, int targetSize)
  25.         {
  26.             Size newSize = new Size();
  27.             if (oldSize.Width > oldSize.Height)
  28.             {
  29.                 newSize.Width = targetSize;
  30.                 newSize.Height = (int)(oldSize.Height * (float)targetSize / (float)oldSize.Width);
  31.             }
  32.             else
  33.             {
  34.                 newSize.Width = (int)(oldSize.Width * (float)targetSize / (float)oldSize.Height);
  35.                 newSize.Height = targetSize;
  36.             }
  37.             return newSize;
  38.         } 
I am passing the targetSize and imageFile byte stream from the front end and streaming out 4 different sizes to be saved into a database for later use. And agian all is working well for large images. But when it comes to protrait and and images smaller than those being thumbnailed it goes astray. I read the post on dontnetslackers.com and changed a little in the CalculateSize method. Heres what happens:

Case 1: Images smaller than the thumbnailed size (small 100px, medium 250px, large 500px, and xlarge 700px respectivly) expand the width to the target size and the height gets chopped off.
Samples: Original, large, xLarge

Case 2: Images that are portrait and the width is smaller than the target size, expand the width to the target size and grow the height to meet the new width respectivly.
Samples: Original, large, xLarge

What I'm needing to do:

Case 1, If the width is smaller than the target size I want to keep the existing width, and height.
Case 2, Adjust the height to be the new target size wile maintaining the correct aspect ratio.

Any help would be grand. Also currently I do not have cacheing in place for the UI to use but that will come with time (maybe). Currently the system operates solely from the database with no dependance on the file system and I'm trying to keep it that way hence why I'm storing the thumbs in the database.

Thanks,
Tim
Aug 11 '08 #1
5 1517
Plater
7,872 Expert 4TB
Is there a reason why you don't use the built in .GetThumbnailImage() function?
Aug 11 '08 #2
tmeers
16
The goal is to create images with an aspect ratio that is the same as the original image. Also doing it this way I can tweak the quality of the generated image and crank it as high or low as I need it to be, from garbage windows 3.1 looking images to as perfect as the original (or darn close).

The code works perfect for the 100px and 250px images, it's just when it starts to get larger than the original in width or height that it gets the glitch.

Tim
Aug 11 '08 #3
Plater
7,872 Expert 4TB
Well in those situations that you mentioned, could you use DrawImageUnscaled() to your advantage?
Aug 11 '08 #4
tmeers
16
Maybe? I'm really not sure because the images are generated correctly when the original is at least as large as the reduced sized image. Correct examples: Large http://gettinlucky.dyndns.org/large/612, Original http://gettinlucky.dyndns.org/612.ashx

How would the DrawImageUnscaled() even work?
Tim
Aug 11 '08 #5
Plater
7,872 Expert 4TB
Actually I guess I am still not clear what you want to do with the special cases.
I understand that if the original image is smaller then what you are trying to make, it can do wierd streching things.
What I would propose is that you compute the largest inscrbibed rectangle that matches the correct aspect ratio of the original image, but that still fits withen the thumbnail size bounds. Draw you image to that size(with stretching) and the draw it UNSCALED onto a blank image of the thumbnail size.
This will produce a white(or whatever background color you want) border in places where the original image did match the thumbnail size exactly.
Kind of like the way letterboxing works when you watch a widescreen format video on a regular tv.
Aug 11 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mekon | last post by:
I have never written a line of script in my life but I need some help with it now. I have this auto generated code which I want to modify if possible The script generates a strip of...
5
by: Deepster | last post by:
Hi all, I have searched the internet for this but havent found what I am looking for exactly, found similar items but I am not that familiar with asp to go and do this myself. So here I am .......
8
by: Michael Satterwhite | last post by:
I'm opening a page that has a single image on it. I'd like to resize the window containing the image to the size of the image (slightly larger OK, not smaller). The width isn't a problem, but the...
2
by: PhoenixFalconhand | last post by:
Begginners' question: I am trying to make a small webform application that doesn't use frames. The application is supposed to show some thumbnails on the left, and whenever you click on one of...
4
by: dgk | last post by:
I have an app that has many subdirectories containing one or more images (jpg, gif, bmp). When the user selects a directory, my plan is to show the images if there are only one or two, or show...
14
by: Rudy | last post by:
Hello all! I been trying to get a handle with Images. I have learned alot from the fine people here. So, I also learned that thumbnail images look terrible taken from a digital cam. I know why...
8
by: Nak | last post by:
Hi there, I've come across quite an annoying bug after optimizing some of my code (which sounds quite strange I know), but take this for a description of my problem. * I have a background...
4
by: RE Kochanski | last post by:
I have attempted to use the CSS techniques from two or three sites to create a CSS only image gallery. I am muddling the affair by placing the thumbnails in one float, the page text in another...
6
by: RoseW | last post by:
This is a collection of images with its own style sheet to create the hover over the thumbnail and the larger image appears. http://www4.webng.com/chesleyhs/images/planting/Planting.html Narrow...
3
by: Danny Ni | last post by:
Hi, I am looking for a way to display images with different aspect ratio into frames with fixed width and height, the problem is some images will look distorted if they are forced into fixed...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.