Solved

Resizing an image in native code

Posted on 2014-10-16
4
238 Views
Last Modified: 2014-10-16
I load an image from a file. It's a multi page TIFF file:

Dim oImg As System.Drawing.Image
Dim i As Integer

oImg = System.Drawing.Image.FromFile(mstrFileName)
i = (some page number)
oImg.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, i)

Now I want to resize the image width and height and save the resulting stretched image as a .jpg or whatever. I thought this would be simple, but there are so many people trying to sell 3rd party controls out there, and so many people using Picture Boxes and whatnot that I can't find the example I'm looking for as I'm getting drowned in the noise. I don't want to use any UI components. I don't want to use any 3rd party components. I want this in vanilla, non-UI based VB.NET code... please

C# is acceptable ... at a push.
0
Comment
Question by:WernerVonBraun
  • 4
4 Comments
 
LVL 4

Author Comment

by:WernerVonBraun
Comment Utility
Never mind. I got it
0
 
LVL 4

Accepted Solution

by:
WernerVonBraun earned 0 total points
Comment Utility
Dim intNumberOfPages As Integer
        Dim intPageNo as Integer
        Dim oImg As Image
        Dim oBmp As Bitmap
        Dim oGrap As Graphics

        'Get Page Number 4 (0 based)
        intPageNo = 3

        oImg = Image.FromFile("your File Name.TIF")
        intNumberOfPages = oImg.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page)

        oImg.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, intPageNo)
        Select Case True
            Case oImg.VerticalResolution < oImg.HorizontalResolution
                oBmp = New Bitmap(CInt(oImg.Width), CInt(Int(oImg.Height * oImg.HorizontalResolution / oImg.VerticalResolution)))
                oGrap = Graphics.FromImage(oBmp)
                oGrap.DrawImage(oImg, 0, 0, oBmp.Width, oBmp.Height)
                oBmp.Save("your target file.png", ImageFormat.Png)
                oBmp.Dispose()
                oGrap.Dispose()
            Case oImg.HorizontalResolution < oImg.VerticalResolution
                oBmp = New Bitmap(CInt(Int(oImg.Width * oImg.VerticalResolution / oImg.HorizontalResolution)), CInt(oImg.Height))
                oGrap = Graphics.FromImage(oBmp)
                oGrap.DrawImage(oImg, 0, 0, oBmp.Width, oBmp.Height)
                oBmp.Save("your target file.png", ImageFormat.Png)
                oBmp.Dispose()
                oGrap.Dispose()
            Case Else
                oImg.Save("your target file.png", ImageFormat.Png)
        End Select

        oImg.Dispose()
        oImg = Nothing
0
 
LVL 4

Author Comment

by:WernerVonBraun
Comment Utility
(obviously you'd normally do something like iterate intPageNo from 0 to intNumberOfPages - 1, but this is enough to illustrate the solution)

So basically, if the horizontal and the vertical resolution of the TIF pages aren't the same, it stretches the smaller of the two to match the larger of the two, rendering the images in the originally intended dimensions. Job done.
0
 
LVL 4

Author Closing Comment

by:WernerVonBraun
Comment Utility
Aren't I great? w00t. LOL
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now