+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 12

Thread: C# Tutorial: Cropping images and bitmaps

  1. #1
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,379

    Cool C# Tutorial: Cropping images and bitmaps

    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.

    Code:
    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.

    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); 
    To show the results, my cropped image is put back into picturebox1. This is third code statement.

    Code:
    pictureBox1.Image croppedBitmap
    Output to show that it works

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

    C# Tutorial: Cropping images and bitmaps-sshot-1.jpg C# Tutorial: Cropping images and bitmaps-sshot-2.jpg C# Tutorial: Cropping images and bitmaps-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: Cropping Images Prototype.zip

    With regards to Siten, my old friend...
    Last edited by ArekBulski; 08-14-2009 at 04:52 PM.

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,750
    Blog Entries
    97

    Re: C# Tutorial: Cropping images and bitmaps

    Short and sweet! Nice tutorial. +rep

  3. #3
    Programming Expert relapse is an unknown quantity at this point relapse's Avatar
    Join Date
    Jul 2009
    Location
    Intrawebs
    Posts
    481
    Blog Entries
    2

    Re: C# Tutorial: Cropping images and bitmaps

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

    Turk4n Rocks

  4. #4
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,379

    Re: C# Tutorial: Cropping images and bitmaps

    Quote Originally Posted by Jordan View Post
    Short and sweet! Nice tutorial. +rep
    Quote Originally Posted by relapse View Post
    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.

  5. #5
    Programming Expert relapse is an unknown quantity at this point relapse's Avatar
    Join Date
    Jul 2009
    Location
    Intrawebs
    Posts
    481
    Blog Entries
    2

    Re: C# Tutorial: Cropping images and bitmaps

    wtf is +rep?
    Being Poor Sucks

    Turk4n Rocks

  6. #6
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    37
    Posts
    13,155
    Blog Entries
    59

    Re: C# Tutorial: Cropping images and bitmaps

    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

  7. #7
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    17
    Posts
    9,428
    Blog Entries
    5

    Re: C# Tutorial: Cropping images and bitmaps

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

  8. #8
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,379

    Re: C# Tutorial: Cropping images and bitmaps

    Quote Originally Posted by Vswe View Post
    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?

  9. #9
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    17
    Posts
    9,428
    Blog Entries
    5

    Re: C# Tutorial: Cropping images and bitmaps

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

  10. #10
    Guru ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski is just really nice ArekBulski's Avatar
    Join Date
    Mar 2009
    Posts
    1,379

    Re: C# Tutorial: Cropping images and bitmaps

    Quote Originally Posted by Vswe View Post
    You know you only can receive 15+rep at the time?
    I had no idea. Thanks for telling. But no rep... *sniff*

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)