Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C# PictureBox

PictureBox pb = new PictureBox();
pb.Image = ... // 3000x3000 image
pb.Size = new Size(200,200);
pb.SizeMode = Zoom;

How to get the viewed 200x200 image handle?

Note:
pb.DrawToBitmap is very slow, and its result is not the viewed image itself.


What I have tried:

Try method 'DrawToBitmap', but it is very slow, and its result is not the viewed image itself.
Posted
Updated 18-Jan-23 1:49am

1 solution

You are making a thumbnail? Why not use the framework? This is my code:
C#
/// <summary>
/// Create a thumbnail image.
/// </summary>
/// <param name="image">Source image</param>
/// <param name="width">Width of thumbnail</param>
/// <param name="height">Height of thumbnail</param>
public static Image GetThumbnailImage(Image image, int width, int height)
    {
    Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
    return image.GetThumbnailImage(width, height, myCallback, IntPtr.Zero);
    }
/// <summary>
/// This is not used in GDI+ 1.0, but must be supplied to some GDI calls.
/// E.g. Image.GetThumbnailImage
/// </summary>
/// <returns>false always</returns>
private static bool ThumbnailCallback()
    {
    return false;
    }
 
Share this answer
 
Comments
w14243 18-Jan-23 3:12am    
GetThumbnailImage will also cost hundreds milliseconds, same as DrawToBitmap.

Want to get viewed image without costing time.
OriginalGriff 18-Jan-23 4:08am    
Instead of loading a 3000x3000 bitmap into the PictureBox - which takes exactly the same time as creating the 200x200 thumbnail because it uses the same code - create the thumbnail and load that into the PictureBox. Then you will spend the time once for you whole app, instead of every single time the PictureBox needs to be displayed ...
BillWoodruff 18-Jan-23 5:49am    
Amen !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900