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

Thread: C# Tutorial: Cropping images and bitmaps

  1. #1
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    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 05:52 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: C# Tutorial: Cropping images and bitmaps

    Short and sweet! Nice tutorial. +rep

  4. #3
    relapse's Avatar
    relapse is offline Programming Expert
    Join Date
    Jul 2009
    Location
    Intrawebs
    Posts
    479
    Blog Entries
    2
    Rep Power
    0

    Re: C# Tutorial: Cropping images and bitmaps

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

  5. #4
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    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.

  6. #5
    relapse's Avatar
    relapse is offline Programming Expert
    Join Date
    Jul 2009
    Location
    Intrawebs
    Posts
    479
    Blog Entries
    2
    Rep Power
    0

    Re: C# Tutorial: Cropping images and bitmaps

    wtf is +rep?

  7. #6
    Join Date
    Jul 2006
    Posts
    16,466
    Blog Entries
    74
    Rep Power
    143

    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.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  8. #7
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: C# Tutorial: Cropping images and bitmaps

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

  9. #8
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    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?

  10. #9
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: C# Tutorial: Cropping images and bitmaps

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

  11. #10
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    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 12 LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 213
    Last Post: 04-14-2011, 07:57 PM
  2. Tutorial: Storing Images in MySQL with PHP
    By Jordan in forum PHP Tutorials
    Replies: 47
    Last Post: 01-04-2011, 08:37 PM
  3. Graphics in VB.NET Part 1 - Bitmaps
    By Vswe in forum Visual Basic Tutorials
    Replies: 3
    Last Post: 11-07-2009, 05:59 PM
  4. Comparing two monochrome bitmaps
    By brownhead in forum C and C++
    Replies: 9
    Last Post: 08-14-2009, 04:20 AM
  5. CSS and Images
    By Chan in forum JavaScript and CSS
    Replies: 5
    Last Post: 10-26-2006, 01:38 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts