?
Solved

Problems cropping an image

Posted on 2006-10-19
3
Medium Priority
?
180 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
[X]
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
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1200 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

Independent Software Vendors: 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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

752 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