Thread: Best way to draw thumbnails?

  1. #1
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101

    Best way to draw thumbnails?

    Hello...I'm trying to speed up the thumbnail loads for a photo app. Is this the best way to draw a thumbnail? There's about 3 other functions involved but I'm just wondering about this particular one.



    Code:
    private static void PrintImage(object o, PrintPageEventArgs e)
    
            {
    
                var img = new Bitmap(printImagePath);
    
                var p = new Point(10, 10);
    
                e.Graphics.DrawImage(img, p);
    
                img.Dispose(); //dispose of the image!!!!
    
            }


    Thanks for any help.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    That's not really showing a thumbnail...that's just showing the image at a particular location. Shouldn't you have some scaling in there?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    Here is the method I use ( to create a thumbnail from a source image's bytes ):

    Code:
            byte[] GetFileThumbnail(byte[] srcImage, System.Drawing.Imaging.ImageFormat imageFormat)
            {
                 System.IO.MemoryStream ms = new MemoryStream(srcImage);
                System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
                System.Drawing.Image thumbnailImage = image.GetThumbnailImage(64, 64, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
                
                using (MemoryStream ms2 = new MemoryStream()) 
                { 
                    thumbnailImage.Save(ms2,imageFormat);
                    thumbnailImage.Dispose();
                    image.Dispose();
                    return ms2.ToArray();
                } 
            }
    
    public bool ThumbnailCallback()
            {
                return true;
            }

    Basically you create a thumbnail for each image file using the above method, then draw the thumbnail image using the same method you would draw any image.
    Last edited by Serapth; 11-23-2011 at 03:35 PM. Reason: Add thumbnailCallback()

  4. #4
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    Hmmm not sure...lemme check the functions calling it...

  5. #5
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    Here's the scaling:

    Code:
                {
                    Image tempImage = Image.FromFile(imageData.Path);
                    //imageData.size = tempImage.Size;
                    //_albumData.setData(imageData, id);
                    imageData.Size.Height = tempImage.Height;
                    imageData.Size.Width = tempImage.Width;
                    newPanel.BackgroundImage = Utilities.ScalImage(tempImage,
                                                                   new Size(_frameSize.Width*thumbNailQuality,
                                                                            _frameSize.Height*thumbNailQuality));
                    //imageData.size = newPanel.BackgroundImage.Size;
                    tempImage.Dispose();
                }

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    The better way is to not use the C# way. It's awfully slow.

    ImageFast: A stand-alone library for quickly loading GDI+ images without validation. - Justin Rogers

    After loading it you can then use this link:

    [C#] Fast Acces To Bitmap Pixels? - C# | Dream.In.Code

    I've used this class wonderfully, including heavy editing and it just blows the C# image handling out of the water.
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  7. #7
    Registered User verbity's Avatar
    Join Date
    Nov 2006
    Posts
    101
    How do you call that thing?

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Can't properly remember, not at my computer. But use those classes in your project. Then you can use inheritance to expand the individual functionality.

    Code:
    unsafebitmap ub = new ub(imagefast.fromfile(filepath));
    Excuse the type case!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Draw on top of everything.
    By DDAZZA in forum C++ Programming
    Replies: 5
    Last Post: 06-04-2009, 11:50 PM
  2. How Can I draw What I want?
    By CChakra in forum Game Programming
    Replies: 22
    Last Post: 09-11-2008, 08:38 AM
  3. Which is the better way to draw?
    By g4j31a5 in forum Game Programming
    Replies: 16
    Last Post: 01-22-2007, 11:56 PM
  4. With what to draw?
    By LMZ in forum C# Programming
    Replies: 0
    Last Post: 07-06-2006, 08:24 AM
  5. My 1st MFC - Draw
    By geek@02 in forum Windows Programming
    Replies: 2
    Last Post: 05-08-2004, 09:51 PM