Home All Groups Group Topic Archive Search About
Author
25 May 2009 4:31 AM
John
Hi

How can I create a bitmap from a JPG file scaled to a certain height and
width? I have checked in help and found Bitmap.Bitmap(Image, Size)
constructor but can't seem to find an example on how to use it.

Thanks

Regards

Author
25 May 2009 5:40 AM
Onur_Güzel
On May 25, 7:31 am, "John" <i...@nospam.infovis.co.uk> wrote:
> Hi
>
> How can I create a bitmap from a JPG file scaled to a certain height and
> width? I have checked in help and found Bitmap.Bitmap(Image, Size)
> constructor but can't seem to find an example on how to use it.
>
> Thanks
>
> Regards

Hi,

You can also use Bitmap(Image, width, height) constructor to specify
new size parameters seperately as follows:

Imports System.Drawing
' Use width=300, height=400 for example
Dim mybitmap As New Bitmap(Image.FromFile("c:\image.jpg"), 300, 400)

or...

Dim mysize As New Size(300, 400)
Dim mybitmap2 As New Bitmap((Image.FromFile("c:\image.jpg")), mysize)

Image class's FromFile method gathers file from specified file
location and used for passing the image as first parameter of Bitmap
class, the rest are size's parameters as seen above.

Note: Image class also provides getting image using FromHBitmap and
FromStream static (shared) methods as well as FromFile method.

HTH,

Onur Güzel
Author
25 May 2009 6:21 AM
John
Hi

Many thanks. Is the scaling proportional? If not, is there a way to maintain
image height and width ratio during scaling?

Thanks again.

Regards

"Onur Güzel" <kimiraikkone***@gmail.com> wrote in message
news:8304d1dd-efbb-4cfd-be37-eec48dcd3c0e@s12g2000yqi.googlegroups.com...
On May 25, 7:31 am, "John" <i...@nospam.infovis.co.uk> wrote:
> Hi
>
> How can I create a bitmap from a JPG file scaled to a certain height and
> width? I have checked in help and found Bitmap.Bitmap(Image, Size)
> constructor but can't seem to find an example on how to use it.
>
> Thanks
>
> Regards

Hi,

You can also use Bitmap(Image, width, height) constructor to specify
new size parameters seperately as follows:

Imports System.Drawing
' Use width=300, height=400 for example
Dim mybitmap As New Bitmap(Image.FromFile("c:\image.jpg"), 300, 400)

or...

Dim mysize As New Size(300, 400)
Dim mybitmap2 As New Bitmap((Image.FromFile("c:\image.jpg")), mysize)

Image class's FromFile method gathers file from specified file
location and used for passing the image as first parameter of Bitmap
class, the rest are size's parameters as seen above.

Note: Image class also provides getting image using FromHBitmap and
FromStream static (shared) methods as well as FromFile method.

HTH,

Onur Güzel
Author
25 May 2009 11:10 AM
Göran_Andersson
John wrote:
> Hi
>
> Many thanks. Is the scaling proportional? If not, is there a way to maintain
> image height and width ratio during scaling?
>
> Thanks again.
>
> Regards
>

No, the image is just stretched/shrunk to the size.

If you want it proportional, it's a bit more work and a bit more math.

Create a Bitmap object of the size you want, the create a Graphics
object to use for drawing on it using the Graphics.FromImage method. Use
the DrawImage method to draw the image onto the new bitmap at the
position and size you want.


--
Göran Andersson
_____
http://www.guffa.com
Author
25 May 2009 5:05 PM
John
Sounds complicated. Any code example somewhere I can look at?

Many Thanks

Regards

Show quoteHide quote
"Göran Andersson" <gu***@guffa.com> wrote in message
news:OX%23zYlS3JHA.4880@TK2MSFTNGP03.phx.gbl...
> John wrote:
>> Hi
>>
>> Many thanks. Is the scaling proportional? If not, is there a way to
>> maintain image height and width ratio during scaling?
>>
>> Thanks again.
>>
>> Regards
>>
>
> No, the image is just stretched/shrunk to the size.
>
> If you want it proportional, it's a bit more work and a bit more math.
>
> Create a Bitmap object of the size you want, the create a Graphics object
> to use for drawing on it using the Graphics.FromImage method. Use the
> DrawImage method to draw the image onto the new bitmap at the position and
> size you want.
>
>
> --
> Göran Andersson
> _____
> http://www.guffa.com
Author
29 May 2009 6:45 AM
Göran_Andersson
John wrote:
> Sounds complicated. Any code example somewhere I can look at?
>
> Many Thanks
>
> Regards

Not very complicated...

// create bitmap
Bitmap dest = new Bitmap(100, 100);

// calculate position
int x, y, w, h;
if (source.Width > source.Height) {
    w = 100;
    h = 100 * source.Height / source.Width;
} else {
    h = 100;
    w = 100 * source.Width / source.Height;
}
x = (100 - w) / 2;
y = (100 - h) / 2;

// draw on bitmap
using (Graphics g = Graphics.FromImage(dest)) {
    g.DrawImage(source, x, y, w, h);
}

--
Göran Andersson
_____
http://www.guffa.com
Author
25 May 2009 8:40 AM
Göran_Andersson
Onur Güzel wrote:
Show quoteHide quote
> On May 25, 7:31 am, "John" <i...@nospam.infovis.co.uk> wrote:
>> Hi
>>
>> How can I create a bitmap from a JPG file scaled to a certain height and
>> width? I have checked in help and found Bitmap.Bitmap(Image, Size)
>> constructor but can't seem to find an example on how to use it.
>>
>> Thanks
>>
>> Regards
>
> Hi,
>
> You can also use Bitmap(Image, width, height) constructor to specify
> new size parameters seperately as follows:
>
> Imports System.Drawing
> ' Use width=300, height=400 for example
> Dim mybitmap As New Bitmap(Image.FromFile("c:\image.jpg"), 300, 400)
>
> or...
>
> Dim mysize As New Size(300, 400)
> Dim mybitmap2 As New Bitmap((Image.FromFile("c:\image.jpg")), mysize)

You should do that in two separate steps, so that you have a reference
to the loaded image so that you can dispose it properly:

Dim mybitmap2 As Bitmap
Using source As Image = Image.FromFile("c:\image.jpg")
    mybitmap2 = New Bitmap(source, mysize)
End Using

> Image class's FromFile method gathers file from specified file
> location and used for passing the image as first parameter of Bitmap
> class, the rest are size's parameters as seen above.
>
> Note: Image class also provides getting image using FromHBitmap and
> FromStream static (shared) methods as well as FromFile method.
>
> HTH,
>
> Onur Güzel


--
Göran Andersson
_____
http://www.guffa.com