Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 928
  • Last Modified:

Crop Image using VB.Net

Hi to all,

I need to crop images based on the X and Y value. I also have the width and height.

How would I go about in doing that?

Thanks
0
ALawrence007
Asked:
ALawrence007
  • 2
  • 2
1 Solution
 
abelCommented:
I took a code snippet I used for a similar question here at EE recently and adjusted it a bit. Here you go, Picture1 is a Picture object on your form:

Dim bmpTemp As New Bitmap(Picture1.Image)
Dim graph As Graphics = Graphics.FromImage(bmpTemp)
 
' rectangle you want to cut out '
Dim rect As New Rectangle(10, 10, 100, 100)
 
' clip / cutout image '
graph.DrawImageUnscaledAndClipped(Picture1.Image, rect)
 
' put back into image, you may need to erase it first '
Picture1.Image = bmpTemp

Open in new window

0
 
ALawrence007Author Commented:
Abel,

I tried the code you supplied, but all it does is display the original image to me. am I missing something?

Thanks
0
 
ALawrence007Author Commented:
Abel,
I got it working in the end.

Thanks
0
 
abelCommented:
> but all it does is display the original image to me. am I missing something?
You either missed to clear and redraw the image, or to paint something else (a background) in the parts you did not cover, otherwise the original image stays put. You can also use a different Picture control as a target to see how it works.

Good to hear you got it working. :)
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now