deutschZuid
deutschZuid

Reputation: 1058

Why is the image not being stretched in the picture box?

I am relatively new to image handling in C#. I am trying to scale this dynamically constructed BMP in a picture box, but the image is not being resized at all. I want the BMP to fill the entirety of the picture box. The picture box is 555 x 555. It simply shows the BMP in its original size (100 x 100). Any ideas?

private void Form1_Load(object sender, EventArgs e)
{
    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Graphics gPB = e.Graphics;

    Bitmap bmp = new Bitmap(100, 100);
    Graphics gBmp = Graphics.FromImage(bmp);


    Brush whiteBrush = new SolidBrush(Color.White);
    gBmp.FillRectangle(whiteBrush, 0, 0, bmp.Width, bmp.Height);

    Brush redBrush = new SolidBrush(Color.IndianRed);
    gBmp.FillRectangle(redBrush, 0, 0, 20f, 20f);

    Brush greenBrush = new SolidBrush(Color.MediumSeaGreen);
    gBmp.FillRectangle(greenBrush, 20, 0, 20f, 20f);

    Brush blueBrush = new SolidBrush(Color.MediumSlateBlue);
    gBmp.FillRectangle(blueBrush, 0, 20, 20f, 20f);

    Brush yellowBrush = new SolidBrush(Color.LightYellow);
    gBmp.FillRectangle(yellowBrush, 20, 20, 20f, 20f);

    gPB.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);

    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}

I have also tried the Zoom SizeMode. That didn't make any difference.

Upvotes: 0

Views: 1181

Answers (1)

gws2
gws2

Reputation: 609

You can tap into .NET's Interpolation property. See here: http://msdn.microsoft.com/en-us/library/system.drawing.graphics.interpolationmode.aspx

gPB.InterpolationMode = InterpolationMode.NearestNeighbor;
gPB.DrawImage(bmp, 0, 0, 555, 555);

Upvotes: 2

Related Questions