Solved

Resizing an image in native code

Posted on 2014-10-16
4
241 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
ID: 40384358
Never mind. I got it
0
 
LVL 4

Accepted Solution

by:
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)
                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
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.
0
 
LVL 4

Author Closing Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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!
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

776 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