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
Solved

Problems cropping an image

Posted on 2006-10-19
3
177 Views
Last Modified: 2011-10-03
I'm having some difficulty cropping an image loaded from a jpg file.
The code I am using below is from another solution found here in Experts Exchange.  Maybe I don't understand GDI+ enough...?
I'm able to save the cropped image, but when I open it, it's just a black image. Any ideas why?  This one goes for 300.

Dim imgTmp As Image = Image.FromFile("C:\auction_photos\5M13370001\5M13370001O.jpg")
Dim bmpOrig As New Bitmap(imgTmp)
imgTmp.Dispose()

Dim rc As RectangleF = bmpOrig.GetBounds(GraphicsUnit.Pixel)

'TODO: adjust the rectangle to your needs
rc.Inflate(-100, 0) 'take 100 px off the right and left

'Create a new bitmap the size of the cropping rectangle
Dim bmpCrop As New System.Drawing.Bitmap(rc.Width, rc.Height, System.Drawing.Imaging.PixelFormat)
imgTmp.Save("c:\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)




0
Comment
Question by:adwooley2
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 300 total points
ID: 17766575
Should be more like...

        Dim imgTmp As Image = Image.FromFile("C:\auction_photos\5M13370001\5M13370001O.jpg")
        Dim bmpCrop As New System.Drawing.Bitmap(imgTmp.Width - (2 * 100), imgTmp.Height)
        Dim g As Graphics = Graphics.FromImage(bmpCrop)
        g.DrawImage(imgTmp, New Point(-100, 0))
        g.Dispose()
        imgTmp.Dispose()
        bmpCrop.Save("c:\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
0
 

Author Comment

by:adwooley2
ID: 17770656
Idle Mind,
Uh... yeah, like that, sure! I just pasted your code in and it works of the bat. THANKS!!!!

adwooley2
0
 

Author Comment

by:adwooley2
ID: 17770720
Idle-Mind,
Am posting a followup question.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pull multiple cvs files into one access table 28 69
Visual Basic Excel Formatting error 4 127
Help me. 3 60
Automatic Email Reminder 4 58
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 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