Click to See Complete Forum and Search --> : [2.0] Resizing bitmap
Icyculyr
Dec 22nd, 2007, 04:50 PM
Bitmap bmp = new Bitmap(tileImageList[currentMap[i, k]]);
I need to change that from 100 * 100, to 50 * 50
Any idea's how?
the bitmap is an image of mine..
jmcilhinney
Dec 23rd, 2007, 12:27 AM
You can't resize an existing Bitmap object. You have to create a new Bitmap object using the constructor that takes dimensions, create a Graphics object for it, then use the appropriate overload of Garphics.DrawImage to draw the original Bitmap onto the new one at the desired location with the desired size. The original will be scaled accordingly.
Icyculyr
Dec 23rd, 2007, 01:33 AM
Thanks
Icyculyr
Dec 25th, 2007, 01:51 AM
Well, I have tested it with an image, and it cuts the image off, is my code incorrect?
private Bitmap resizeBitmap(Bitmap bitmap, Size newSize)
{
Bitmap bmp = new Bitmap(newSize.Width, newSize.Height);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(bitmap, new Point(0, 0));
bitmap.Dispose();
bitmap = null;
g.Dispose();
g = null;
return bmp;
}
Thanks
Fromethius
Dec 25th, 2007, 01:20 PM
Try this:
private Bitmap resizeBitmap(Bitmap bitmap, Size newSize)
{
Bitmap bmp = new Bitmap(newSize.Width, newSize.Height);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(bitmap, new Rectangle(Point.Empty, newSize));
bitmap.Dispose();
bitmap = null;
g.Dispose();
g = null;
return bmp;
}
Fromethius
Dec 25th, 2007, 01:30 PM
Also, for such things where you would like to change an existing variable, it is often cumbersome to have to declare a new variable just to hold the new bitmap. In this case, the ref keyword shines. If I were you, I would tackle the problem like this:
private void ResizeBitmap(ref Bitmap sourceBitmap, Size destinationSize)
{
Bitmap newBmp = new Bitmap(destinationSize.Width, destinationSize.Height);
Graphics graphics = Graphics.FromImage(newBmp);
graphics.DrawImage(sourceBitmap, new Rectangle(Point.Empty, destinationSize));
sourceBitmap = newBmp;
}
Where calling it would be something like this:
Bitmap bmpToResize = new Bitmap(@"C:\Documents and Settings\Fromethius\My Documents\My Pictures\Character.bmp");
ResizeBitmap(ref bmpToResize, new Size(70, 70));
e.Graphics.DrawImage(bmpToResize, Point.Empty);
Icyculyr
Dec 25th, 2007, 05:50 PM
Thanks, @ your second post, my images are from my Resources; not a file path..
Fromethius
Dec 25th, 2007, 06:13 PM
Err.. It doesn't really matter. It was just an example. Just supply a Bitmap as the parameter..
:ehh:
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.