|
-
Dec 22nd, 2007, 05:50 PM
#1
Thread Starter
Frenzied Member
[2.0] Resizing bitmap
Code:
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..
-
Dec 23rd, 2007, 01:27 AM
#2
Re: [2.0] Resizing bitmap
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.
-
Dec 23rd, 2007, 02:33 AM
#3
Thread Starter
Frenzied Member
Re: [2.0] Resizing bitmap
-
Dec 25th, 2007, 02:51 AM
#4
Thread Starter
Frenzied Member
Re: [2.0] Resizing bitmap
Well, I have tested it with an image, and it cuts the image off, is my code incorrect?
Code:
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
-
Dec 25th, 2007, 02:20 PM
#5
Frenzied Member
Re: [2.0] Resizing bitmap
Try this:
c# Code:
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;
}
-
Dec 25th, 2007, 02:30 PM
#6
Frenzied Member
Re: [2.0] Resizing bitmap
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:
Code:
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:
Code:
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);
-
Dec 25th, 2007, 06:50 PM
#7
Thread Starter
Frenzied Member
Re: [2.0] Resizing bitmap
Thanks, @ your second post, my images are from my Resources; not a file path..
-
Dec 25th, 2007, 07:13 PM
#8
Frenzied Member
Re: [2.0] Resizing bitmap
Err.. It doesn't really matter. It was just an example. Just supply a Bitmap as the parameter..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|