Click to See Complete Forum and Search --> : Scale one Bitmap into another (just the inner quardatic part)


thegrinch
December 23rd, 2005, 04:40 AM
Hi!
I would like to have a method:

static Bitmap * scale (Bitmap * b, int width, int height)

It shall do the following transformation:
Assume b has Dimensions (1280,1024) and width and height are equal to 512.
Ok. Then, the Method shall take the centered quadratic part of b (which is 1024x1024 px in size, starting at Point ( (1280-1024)/2 , 0) ), and scale it to 512 x 512. Finally a new Bitmap containing this scaled image shall be returned.

My searches lead to this code up to now, but I could not find a way how to convert the Graphics into a Bitmap:

Graphics * gfx = Graphics::FromImage(new Bitmap(512,512);
gfx->DrawImage(b,0,0,512,512);


Big thanks for your Help!

so long

ovidiucucu
December 23rd, 2005, 06:25 AM
Although it's possible to achieve what you want by using a Graphics object, here is an easier method:

Call Bitmap::Clone to get a portion of the original bitmap
Call Image::GetThumbnailImage to scale it.

thegrinch
December 23rd, 2005, 07:31 AM
Thank you for your answer. I've now been trying for one hour getting this code to compile:

bool ThumbnailCallback()
{
return false;
}

System::Drawing::Bitmap * utility::FitBitmap(System::Drawing::Bitmap * _org)
{
float x;
System::Drawing::Bitmap * bReturn;
System::Drawing::Rectangle rect;
if (_org->Width > _org->Height)
{
x = (_org->Width - _org->Height) / 2;
rect = Rectangle(x,0,x+_org->Height, _org->Height);
bReturn = _org->Clone(rect,_org->PixelFormat);
}
else
{
x = (_org->Height - _org->Width) / 2;
rect = Rectangle(0,x,_org->Width, x+_org->Width);
bReturn = _org->Clone(rect,_org->PixelFormat);
}
Image::GetThumbnailImageAbort* myCallback = new Image::GetThumbnailImageAbort( &ThumbnailCallback);
return (Bitmap*)bReturn->GetThumbnailImage(512,512, myCallback, IntPtr::Zero);
}


error msg is: "d:\fopra\xmas_edition\utility.cpp(37): error C3350: 'System::Drawing::Image::GetThumbnailImageAbort' : a delegate constructor expects two arguments"

but in msdn, they just use one argument.

Please help!
thx in advance

ovidiucucu
December 23rd, 2005, 08:33 AM
The third argument of Image::GetThumbnailImage is optional (default value is NULL).
You can simply write something like
/* ... */ pReturn->GetThumbnailImage(512,512);

thegrinch
December 23rd, 2005, 08:48 AM
are you sure?

d:\fopra\xmas_edition\utility.cpp(38): error C2660: 'System::Drawing::Image::GetThumbnailImage' : function does not take 2 arguments

ovidiucucu
December 23rd, 2005, 10:14 AM
Yep. If using C++ I'm sure.