Resizing an image in native code

Posted on 2014-10-16
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.
Question by:WernerVonBraun
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4

Author Comment

ID: 40384358
Never mind. I got it

Accepted Solution

WernerVonBraun earned 0 total points
ID: 40384396
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)
            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)
            Case Else
                oImg.Save("your target file.png", ImageFormat.Png)
        End Select

        oImg = Nothing

Author Comment

ID: 40384403
(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.

Author Closing Comment

ID: 40384411
Aren't I great? w00t. LOL

Featured Post

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

622 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