Solved

Resizing an image in native code

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

679 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