Short and sweet! Nice tutorial. +rep
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.
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.Code:Bitmap croppedBitmap = new Bitmap(pictureBox1.Image);
My margins are provided by NumericUpDown controls, therefore I had to convert them from double to integer, which adds these "(int)" explicit conversions.
To show the results, my cropped image is put back into picturebox1. This is third code statement.Code: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);
Output to show that it worksCode:pictureBox1.Image = croppedBitmap;
Here are screenshots showing two subsequent cropping done on a picture made by NASA.
![]()
![]()
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: Cropping Images Prototype.zip
With regards to Siten, my old friend...
Last edited by ArekBulski; 08-14-2009 at 04:52 PM.
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
Short and sweet! Nice tutorial. +rep
I don't use C# but this seems really cool!
Being Poor Sucks
Turk4n Rocks
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.![]()
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
wtf is +rep?
Being Poor Sucks
Turk4n Rocks
Click on the scales icon next to a post to indicate you think it was particularly good/helpful.
CodeCall Blog | CodeCall Wiki
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
relapse doesn't even have any reppower so it doesn't matter.
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
You know you only can receive 15+rep at the time?
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
There are currently 1 users browsing this thread. (0 members and 1 guests)