drawing image to bigger bitmap

20,305

Solution 1

See the documentation for Graphics.DrawImage. You can specify source and destination rectangles.

Example code:

Image i = Image.FromFile(fileName); // This is 300x300
Bitmap b = new Bitmap(500, 500);

using(Graphics g = Graphics.FromImage(b))
{
    g.DrawImage(i, 0, 0, 500, 500);
}

To use code make sure to add reference to System.Drawing assembly and add using System.Drawing to the file.

Solution 2

You could try using the following:

public Image ImageZoom(Image image, Size newSize)
{
    var bitmap = new Bitmap(image, newSize.Width, newSize.Height);
    using (var g = Graphics.FromImage(bitmap))
    {
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    }

    return bitmap;
}

And choose from one of the available InterpolationModes.

Share:
20,305
yasink
Author by

yasink

Updated on February 10, 2020

Comments

  • yasink
    yasink about 4 years

    Basically I want to stretch smaller image (i.e. 300x300 to bigger one i.e. 500x500) without space or black background.

    I have a bitmap (let's say width 500px, and height 500px). How to draw another (smaller) image on that bitmap so it takes whole bitmap?

    I already know how to create bitmap (i.e. var bitmap = new Bitmap(500, 500);) and get the image - it can be loaded from file (i.e. var image = Image.FromFile(...);) or obtained from some other source.

  • WildCrustacean
    WildCrustacean over 13 years
    any particular reason that you see for why it isn't working? an error? what happens when you run the code?