Image resize using GDI+?

J

John Swan

Hello.

I'm trying to create a simple program that amongst other things creates a
thumbnail of an image (Bitmap) to a set size determined by the user in
pixels?
The problem is: All of the examples I have seen so far are in c#.
Can anyone please provide reference to a c++ managed version.

Thanks.
John
 
P

Peter Oliphant

(1) create a PictureBox
(2) set the Image of the PictureBox to your Bitmap (form->Image = bitmap)
(3) add the PictureBox to your form (form->Controls->Add( picturebox ))
(4) set the Width and Height of your PictureBox.

The image should stretch to fill whatever dimensions you givethe PictureBox
(there might be a proprty of PictureBox you have to set a particular way to
get this behavior, but I think it's default is to stretch)...

[==P==]
 
J

John Swan

Sorry. I did not explain correctly.

I would like this to be a batch process so I dont want to actually display
image.
Just create a lot of thumbnails of the images.

Peter Oliphant said:
(1) create a PictureBox
(2) set the Image of the PictureBox to your Bitmap (form->Image = bitmap)
(3) add the PictureBox to your form (form->Controls->Add( picturebox ))
(4) set the Width and Height of your PictureBox.

The image should stretch to fill whatever dimensions you givethe PictureBox
(there might be a proprty of PictureBox you have to set a particular way to
get this behavior, but I think it's default is to stretch)...

[==P==]

John Swan said:
Hello.

I'm trying to create a simple program that amongst other things creates a
thumbnail of an image (Bitmap) to a set size determined by the user in
pixels?
The problem is: All of the examples I have seen so far are in c#.
Can anyone please provide reference to a c++ managed version.

Thanks.
John
 
P

Peter Oliphant

Ok, but now it gets a bit more complex. You could do the following (I
think):

Create an offscreen buffer bitmap. Get the Graphics object for it, use the
Graphics object and its DrawImage method to draw the image on the offscreen
buffer any size you want. Now create a new bitmap as a rectangle the size of
the drawn image on the offscreen, blit the image from offscreen to new
bitmap, and this new bitmap can be used as a thumbnail (and is not drawn on
the screen). Provided its big enough to hold your biggest thumbnail, you can
use the sane offscreen bitmap over and over (provided you Clear it before
each use)...

float m_W = thumb_w ;
float m_H = thumb_h ;
Bitmap^ image = gcnew Bitmap( filename) ;
Bitmap^ m_Offscreen = gcnew Bitmap( thumb_w, thumb_h) ;
Graphics^ m_Offscreen_Graphics = Graphics::FromImage( m_Offscreen ) ;
m_Offscreen_Graphics->DrawImage( image, 0, 0, m_W, m_H ) ;
Bitmap^ thumbnail = gcnew Bitmap( m_W, m_H ) ;
thumbnail->DrawImageUnscaled( m_Offscreen_Graphics, 0, 0 ) ;

[==P==]



John Swan said:
Sorry. I did not explain correctly.

I would like this to be a batch process so I dont want to actually display
image.
Just create a lot of thumbnails of the images.

Peter Oliphant said:
(1) create a PictureBox
(2) set the Image of the PictureBox to your Bitmap (form->Image = bitmap)
(3) add the PictureBox to your form (form->Controls->Add( picturebox ))
(4) set the Width and Height of your PictureBox.

The image should stretch to fill whatever dimensions you givethe PictureBox
(there might be a proprty of PictureBox you have to set a particular way to
get this behavior, but I think it's default is to stretch)...

[==P==]

John Swan said:
Hello.

I'm trying to create a simple program that amongst other things creates a
thumbnail of an image (Bitmap) to a set size determined by the user in
pixels?
The problem is: All of the examples I have seen so far are in c#.
Can anyone please provide reference to a c++ managed version.

Thanks.
John
 
J

John Swan

Just out of interest. I posted this on a different newsgroup and the rely
which worked is as follows.
---------------------------------------------------------
John Swan said:
I'm trying to create a simple program that amongst other things creates a
thumbnail of an image (Bitmap) to a set size determined by the user in
pixels?
The problem is: All of the examples I have seen so far are in c#.
Can anyone please provide reference to a c++ managed version.

C++/CLI sample (requires a reference to "System.Drawing.dll"):

\\\
using namespace System;
using namespace System::Drawing;
using namespace System::Drawing::Drawing2D;
using namespace System::Drawing::Imaging;

int main(array<System::String ^> ^args)
{
Bitmap ^bmp1 = gcnew Bitmap(L"C:\\WINDOWS\\Angler.bmp");
Bitmap ^bmp2 = gcnew Bitmap(
(int)(bmp1->Width * 0.1),
(int)(bmp1->Height * 0.1),
PixelFormat::Format24bppRgb
);
Graphics ^g = Graphics::FromImage(bmp2);
g->InterpolationMode = InterpolationMode::HighQualityBicubic;
g->DrawImage(bmp1, 0, 0, bmp2->Width, bmp2->Height);
bmp2->Save(L"C:\\Test.bmp");
return 0;
}
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
---------------------------------------------------

Turns out that drawimage just draws the image to another image. I think the
term DrawImage is a little ambigious as it isn't actually drawing anything
but it works.

Anyway.
Cheers

Peter Oliphant said:
Ok, but now it gets a bit more complex. You could do the following (I
think):

Create an offscreen buffer bitmap. Get the Graphics object for it, use the
Graphics object and its DrawImage method to draw the image on the offscreen
buffer any size you want. Now create a new bitmap as a rectangle the size of
the drawn image on the offscreen, blit the image from offscreen to new
bitmap, and this new bitmap can be used as a thumbnail (and is not drawn on
the screen). Provided its big enough to hold your biggest thumbnail, you can
use the sane offscreen bitmap over and over (provided you Clear it before
each use)...

float m_W = thumb_w ;
float m_H = thumb_h ;
Bitmap^ image = gcnew Bitmap( filename) ;
Bitmap^ m_Offscreen = gcnew Bitmap( thumb_w, thumb_h) ;
Graphics^ m_Offscreen_Graphics = Graphics::FromImage( m_Offscreen ) ;
m_Offscreen_Graphics->DrawImage( image, 0, 0, m_W, m_H ) ;
Bitmap^ thumbnail = gcnew Bitmap( m_W, m_H ) ;
thumbnail->DrawImageUnscaled( m_Offscreen_Graphics, 0, 0 ) ;

[==P==]



John Swan said:
Sorry. I did not explain correctly.

I would like this to be a batch process so I dont want to actually display
image.
Just create a lot of thumbnails of the images.

Peter Oliphant said:
(1) create a PictureBox
(2) set the Image of the PictureBox to your Bitmap (form->Image = bitmap)
(3) add the PictureBox to your form (form->Controls->Add( picturebox ))
(4) set the Width and Height of your PictureBox.

The image should stretch to fill whatever dimensions you givethe PictureBox
(there might be a proprty of PictureBox you have to set a particular
way
to
get this behavior, but I think it's default is to stretch)...

[==P==]

Hello.

I'm trying to create a simple program that amongst other things
creates
a
thumbnail of an image (Bitmap) to a set size determined by the user in
pixels?
The problem is: All of the examples I have seen so far are in c#.
Can anyone please provide reference to a c++ managed version.

Thanks.
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top