Results 1 to 8 of 8

Thread: [2.0] Resizing bitmap

  1. #1

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    [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..

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2.0] Resizing bitmap

    Thanks

  4. #4

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    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

  5. #5
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] Resizing bitmap

    Try this:

    c# Code:
    1. private Bitmap resizeBitmap(Bitmap bitmap, Size newSize)
    2.         {
    3.             Bitmap bmp = new Bitmap(newSize.Width, newSize.Height);
    4.             Graphics g = Graphics.FromImage(bmp);
    5.             g.DrawImage(bitmap, new Rectangle(Point.Empty, newSize));
    6.             bitmap.Dispose();
    7.             bitmap = null;
    8.             g.Dispose();
    9.             g = null;
    10.             return bmp;
    11.         }

  6. #6
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    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);

  7. #7

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2.0] Resizing bitmap

    Thanks, @ your second post, my images are from my Resources; not a file path..

  8. #8
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    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
  •  



Click Here to Expand Forum to Full Width