Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawing image to bigger bitmap [closed]

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.

like image 876
yasink Avatar asked Oct 07 '10 18:10

yasink


2 Answers

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.

like image 92
WildCrustacean Avatar answered Sep 28 '22 12:09

WildCrustacean


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.

like image 45
Emanuel Varga Avatar answered Sep 28 '22 11:09

Emanuel Varga