Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi, I load Image to the picture box but I have small image I want image take the size of picturebox ,I Know can change it by the properties stretch but I want zooming it by code
C#
OpenFileDialog open = new OpenFileDialog();

            if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Image Background = Image.FromFile(open.FileName);
                Bitmap bitmap = new Bitmap(open.FileName);
                pictureBox1.Image = Background;

                height = bitmap.Height;
                width = bitmap.Width;
Posted
Updated 11-Dec-14 9:18am
v2

1 solution

The solution is simple enough: don't use PictureBox. This control is redundant, designed only for the simplest tasks (showing static images) and won't help you if you want something a bit more complex, such as zooming. It can only add you hassles, waste some extra resources and your development time. Please see my past answers and advise what to do instead:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

Some other answers on graphics rendering:
What kind of playful method is Paint DataGridView,
Capture the drawing on a panel,
Drawing Lines between mdi child forms.

And this is my past answer related to zoom: Zoom image in Csharp net mouse wheel.
Sorry that this answer has too many detail and repeated information. The essential new information is: using Transform and Invalidate. Alternatively, you can use the methods System.Drawing.Graphics.DrawImage which performs re-sampling of the image, which happens where the method expects the size of the destination rectangle and this rectangle size is different from the image size. Please see:
System.Drawing.Graphics.DrawImage.
You will find the explanation on where to use drawing methods in my answers referenced above.

Now, a big warning for you: in re-sampling of the pixel images, only re-sampling down (when you render the image in smaller size) works well. At best, you can zoom the image in (to a bigger size) only slightly, otherwise you will get extremely poor quality. What to do: make you original image as big as possible so your re-sampling would mostly reduce the rendered image size. Also, as to the quality, you will need to leverage this property: http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode%28v=vs.110%29.aspx.
The best quality of rendering is the one you can obtain with System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic:
http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode%28v=vs.110%29.aspx.

—SA
 
Share this answer
 

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