0

I'm trying to resize the image to fit the PictureBox size but it's not working. I added a method resizeImage but no matter what size I give the result is the same.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        Bitmap myBitmap = CreateNonIndexedImage(new Bitmap(@"d:\drawplane1.jpg"));
        resizeImage(myBitmap, new Size(1, 1));

        // Draw myBitmap to the screen.
        e.Graphics.DrawImage(myBitmap, 0, 0, myBitmap.Width,
            myBitmap.Height);

        // Set each pixel in myBitmap to black.
        for (int Xcount = 0; Xcount < myBitmap.Width; Xcount++)
        {
            for (int Ycount = 0; Ycount < myBitmap.Height; Ycount++)
            {
                myBitmap.SetPixel(Xcount, Ycount, Color.Black);
            }
        }

        // Draw myBitmap to the screen again.
        e.Graphics.DrawImage(myBitmap, myBitmap.Width, 0,
            myBitmap.Width, myBitmap.Height);
    }

    public Bitmap CreateNonIndexedImage(Image src)
    {
        Bitmap newBmp = new Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        using (Graphics gfx = Graphics.FromImage(newBmp))
        {
            gfx.DrawImage(src, 0, 0);
        }

        return newBmp;
    }

    public static Image resizeImage(Image imgToResize, Size size)
    {
        return (Image)(new Bitmap(imgToResize, size));
    }
}

No mater what size I put in the line:

resizeImage(myBitmap, new Size(1, 1));

..the image is too big inside the pictureBox: I tried 10,10 then 1,1 for testing but it's not changing the image size.

Image in picturebox

This is the original image :

Original

Original image file : https://i.stack.imgur.com/6uFMo.jpg

Benzi Avrumi
  • 937
  • 1
  • 8
  • 24
  • 1
    Is that the back of the Eurofighter? – Mike Miller Jul 29 '20 at 15:54
  • 1
    If this is just for displaying the image, you don't need to resize the image. The `PictureBox` control gives you control over this out of the box. Use `pictureBox1.SizeMode = PictureBoxSizeMode.Zoom`. You may also use `PictureBoxSizeMode.StretchImage` if you like but I wouldn't recommend that. – 41686d6564 stands w. Palestine Jul 29 '20 at 15:58
  • Does this answer your question? [Fit Image into PictureBox](https://stackoverflow.com/questions/16822138/fit-image-into-picturebox) – 41686d6564 stands w. Palestine Jul 29 '20 at 16:01
  • Nothing with the sizemode is working. I tried in the code and in the editor properties but nothing changed. – Benzi Avrumi Jul 29 '20 at 16:11
  • Could be the method CreateNonIndexedImage make something wrong ? – Benzi Avrumi Jul 29 '20 at 16:12
  • See the `ResizeImage()` method here: [How to crop an elliptical region of an Image with smooth borders](https://stackoverflow.com/a/61554272/7444103) and all the related methods here: [Zoom and translate an Image from the mouse location](https://stackoverflow.com/a/61964222/7444103) (where you can find all you actually need: `GetScaledRect()`, `GetDrawingImageRect()`, `CenterScaledRectangleOnCanvas()`, etc.). The notes here: [Disable Image blending on a PictureBox](https://stackoverflow.com/a/54726707/7444103) can also be useful. – Jimi Jul 29 '20 at 16:25

3 Answers3

2

Try setting the SizeMode of your PictureBox to PictureSizeMode.Zoom or PictureSizeMode.StretchImage and see if it helps. Either through your properties editor or in code.

 pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
msmolcic
  • 6,407
  • 8
  • 32
  • 56
2

You can use the SizeMode property of PictureBox from Properties window and set its value to StrechImage

enter image description here

integer
  • 221
  • 4
  • 13
1

Your code has two issues:

  • You don't use the SizeMode property
  • You draw the image in the Paint event instead of just setting the Image property

If you want to draw the image in Paint (eg. because it is an animation, whose frames are generated rapidly), then you don't need a PictureBox, a simple Panel will also do it.

But if you generate the image once, then just assign it to the Image property, set the SizeMode for scaling and PictureBox will care about the rest

György Kőszeg
  • 17,093
  • 6
  • 37
  • 65
  • If you're drawing an animated sequence, you really don't want to use a Panel. A PictureBox is way better, since it's double-buffered. If you want a *lighter* control, a Flat Label is also double-buffered. `Panel` is not. – Jimi Jul 29 '20 at 17:01
  • Double buffering can be enabled for any control. – György Kőszeg Jul 29 '20 at 17:48
  • Double-buffering can be enabled but a Control as a PictureBox and its internal operations cannot be emulated. Thus, it's not the same thing. I don't see a reason to do that either, since you already have a Control with these features setup and optimized. If one needs a drawing surface with less dependencies, as already mentioned, the Label Control offers the same functionality without any modifications. Some, for unknown reasons, thinks that a PictureBox is *heavier* than a Panel (not considering the Label at all, of course): that's mythology. – Jimi Jul 29 '20 at 18:09
  • PictureBox derives from Control, Panel from `ScrollableControl` and implements `IArrangedElement`: not exactly made for drawing (even though enabling its `OptimizedDoubleBuffer` can be used to create *special effects*, as shown here: [Transparent Overlapping Circular Progress Bars](https://stackoverflow.com/a/53379442/7444103), for example (because if generates a form of *persistence* in its DC), but *less heavy* (whatever that can mean here), not really. – Jimi Jul 29 '20 at 18:18
  • Look, you are right in all of these, but the point wasn't that `Panel` is the _one right choice_ for drawing in `OnPaint` but rather that it has nothing to do with `PictureBox`. And scrolling can be actually useful when you implement custom zooming. However, even my custom zoomable/scrollable [image viewer](https://github.com/koszeggy/KGySoft.Drawing.Tools/blob/5c6d214def68b1e4efd1827f0aa2992ef7f92f1b/KGySoft.Drawing.ImagingTools/View/Controls/ImageViewer.cs#L37) is derived (almost) directly from `Control` but that is another story. – György Kőszeg Jul 30 '20 at 07:37