|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Bitmap from a fileHi
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 On May 25, 7:31 am, "John" <i...@nospam.infovis.co.uk> wrote: Hi,> 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 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 "Onur Güzel" <kimiraikkone***@gmail.com> wrote in message Hi,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 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 John wrote:
> Hi No, the image is just stretched/shrunk to the size.> > Many thanks. Is the scaling proportional? If not, is there a way to maintain > image height and width ratio during scaling? > > Thanks again. > > Regards > 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. 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 John wrote:
> Sounds complicated. Any code example somewhere I can look at? Not very complicated...> > Many Thanks > > Regards // 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); } Onur Güzel wrote:
Show quoteHide quote > On May 25, 7:31 am, "John" <i...@nospam.infovis.co.uk> wrote: You should do that in two separate steps, so that you have a reference >> 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) 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
use variable as textbox name?
treeview in windowsdialog Call button click event is it save to call functions from another form? Bracketed types Using function with PChar data type Visual Studio 2008 and Classes Inheriting From System.Web.UI.WebControls.Style Problem with embedded carriage returns trouble reading word documents still problems reading word documents... |
|||||||||||||||||||||||