Solved

Problems cropping an image

Posted on 2006-10-19
3
174 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
Comment Utility
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
Comment Utility
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
Comment Utility
Idle-Mind,
Am posting a followup question.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

763 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

10 Experts available now in Live!

Get 1:1 Help Now