Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Graphics
  4. [Solved] Cropping empty space around drawing [modified]

[Solved] Cropping empty space around drawing [modified]

Scheduled Pinned Locked Moved Graphics
graphicsquestionwinformshelptutorial
2 Posts 1 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kensai
    wrote on last edited by
    #1

    I have series of images very similar to this example image: http://img258.imageshack.us/img258/2483/exampleep8.gif[^] As you can see there's empty white space around the text and lines. How can I remove this empty space and crop the image? The red line shows where I want to crop it. I'm a newbie when it comes to Drawing/GDI+, I appreciate any help.

    modified on Wednesday, June 11, 2008 8:25 AM

    K 1 Reply Last reply
    0
    • K kensai

      I have series of images very similar to this example image: http://img258.imageshack.us/img258/2483/exampleep8.gif[^] As you can see there's empty white space around the text and lines. How can I remove this empty space and crop the image? The red line shows where I want to crop it. I'm a newbie when it comes to Drawing/GDI+, I appreciate any help.

      modified on Wednesday, June 11, 2008 8:25 AM

      K Offline
      K Offline
      kensai
      wrote on last edited by
      #2

      Problem solved here's the code:

      Bitmap b = new Bitmap(filename);
      Rectangle r = new Rectangle();
      int topLeft = b.Width, topRight = 0, topUpper = b.Height, topBottom = 0;
      string whitePixel = "ffffffff";

      for (int y = 1; y < b.Height; y++)
      {
      for (int x = 1; x < b.Width; x++)
      {
      if (b.GetPixel(x, y).Name != whitePixel)
      {
      if (x < topLeft)
      topLeft = x;

      if (x > topRight)
      topRight = x;

      if (y < topUpper)
      topUpper = y;

      if (y > topBottom)
      topBottom = y;
      }
      }
      }

      r.Location = new Point(topLeft-1, topUpper-1);
      r.Size = new Size((topRight - topLeft)+1, (topBottom - topUpper)+1);
      Bitmap b2 = b.Clone(r, b.PixelFormat);
      b2.Save(newfilename, ImageFormat.Gif);

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups