Bitmap from a file

  • Thread starter Thread starter John
  • Start date Start date
J

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

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

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
 
Onur said:
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
 
Onur said:
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
 
John said:
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.
 
John said:
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.
 
John said:
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);
}
 
John said:
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);
}
 
Back
Top