Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C# Tutorial: Cropping images and bitmaps

php

  • Please log in to reply
13 replies to this topic

#1 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 14 August 2009 - 11:40 AM

Introduction for curious people

I got a nice homework assignment from Siten, to make a program that crops images. It seemed easy at first, but then ...proven otherwise. Therefore let me show you some code that "cuts margins off images", or how Photoshop names it: crops images. :)

Cropping an image (or bitmap)

First of all, you can only crop a Bitmap class. If you have an Image class, you will have to transform it to a Bitmap object. This is my first code statement.


Bitmap croppedBitmap = new Bitmap(pictureBox1.Image);


Cropping a Bitmap may seem hard to implement, probably because it is being done by cloning... yes, Clone() method takes optional arguments that can be smaller than original image. This is second code statement.

My margins are provided by NumericUpDown controls, therefore I had to convert them from double to integer, which adds these "(int)" explicit conversions.


croppedBitmap = croppedBitmap.Clone(
new Rectangle(
(int)LeftMargin.Value, (int)TopMargin.Value,
(int)croppedBitmap.Width - (int)LeftMargin.Value - (int)RightMargin.Value,
(int)croppedBitmap.Height - (int)TopMargin.Value - (int)BottomMargin.Value),
System.Drawing.Imaging.PixelFormat.DontCare);


To show the results, my cropped image is put back into picturebox1. This is third code statement.


pictureBox1.Image = croppedBitmap;


Output to show that it works

Here are screenshots showing two subsequent cropping done on a picture made by NASA.

sshot-3.jpg

Code behind this prototype

You can open the whole solution and play with it as you want. Also you can run a compiled exe without using Visual Studio, just look into Bin\Release\ folder.

Solution code: [ATTACH]1909[/ATTACH]

With regards to Siten, my old friend...

Attached Thumbnails

  • sshot-1.jpg
  • sshot-2.jpg

Attached Files


Edited by ArekBulski, 14 August 2009 - 04:52 PM.

  • 1

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 14 August 2009 - 12:55 PM

Short and sweet! Nice tutorial. +rep
  • 0

#3 relapse

relapse

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 323 posts

Posted 14 August 2009 - 04:39 PM

I don't use C# but this seems really cool!
  • 0

#4 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 14 August 2009 - 04:50 PM

Short and sweet! Nice tutorial. +rep


I don't use C# but this seems really cool!


Thank you, guys! I appreciate your comments. Relapse, I would be interested in getting some +rep, if you would like to give some. I am trying to get enough rep to earn a logo T-Shirt. But thank you anyways! Feels great to be seen as a good coder by others. :)
  • 0

#5 relapse

relapse

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 323 posts

Posted 14 August 2009 - 04:58 PM

wtf is +rep?
  • 0

#6 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 15 August 2009 - 04:11 AM

Click on the scales icon next to a post to indicate you think it was particularly good/helpful.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#7 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 25 August 2009 - 11:07 PM

relapse doesn't even have any reppower so it doesn't matter.
  • 0

#8 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 26 August 2009 - 04:52 AM

relapse doesn't even have any reppower so it doesn't matter.


Uh-oh, I see. You are right there, his rep power is 0. But your rep power is... woow. Wanna use it? :rolleyes:
  • 0

#9 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 26 August 2009 - 05:26 AM

You know you only can receive 15+rep at the time?
  • 0

#10 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 26 August 2009 - 11:28 AM

You know you only can receive 15+rep at the time?


I had no idea. Thanks for telling. But no rep... *sniff* :rolleyes:
  • 0

#11 Charny

Charny

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 55 posts

Posted 10 January 2010 - 03:06 PM

Thanks for this tutorial, it will probably be useful in my next application.
  • 0

#12 gokuajmes

gokuajmes

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 483 posts

Posted 22 January 2010 - 01:06 AM

i could use this for making Graphical effects.Thanks for the tut
How do i + Rep you ?
  • 0





Also tagged with one or more of these keywords: php

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download