Solved

Resizing an image in native code

Posted on 2014-10-16
4
240 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

896 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

18 Experts available now in Live!

Get 1:1 Help Now