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. C#
  4. How can i crop only the used part of a Bitmap?

How can i crop only the used part of a Bitmap?

Scheduled Pinned Locked Moved C#
questiongraphics
8 Posts 5 Posters 0 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.
  • B Offline
    B Offline
    bonzaiholding
    wrote on last edited by
    #1

    If i want to crop a rectangle of the used part of a bitmap, what is the easiest way to do it?

    L L M 3 Replies Last reply
    0
    • B bonzaiholding

      If i want to crop a rectangle of the used part of a bitmap, what is the easiest way to do it?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Just create a new bitmap, get the pixel from original bitmap and set the pixel to new bitmap using GetPixel and SetPixel.

      B 1 Reply Last reply
      0
      • B bonzaiholding

        If i want to crop a rectangle of the used part of a bitmap, what is the easiest way to do it?

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, the one way with decent performance goes like this: - create a new bitmap with appropriate size; - call CreateGraphics on it; - perform Graphics.DrawImage using an overload that lets you choose part of a source image - if necessary, save the bitmap in the format you like - Dispose of the objects you no longer need. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        B 1 Reply Last reply
        0
        • L Lost User

          Just create a new bitmap, get the pixel from original bitmap and set the pixel to new bitmap using GetPixel and SetPixel.

          B Offline
          B Offline
          bonzaiholding
          wrote on last edited by
          #4

          But how can i know where are the empty pixels and where there are used pixel ? For Example: I will get in one of the iteration a Bitmap with 1000X1000 pixels but the DLL that i used only used 15X28 in location (523,724) in the bitmap, How can i get a new Bitmap with the size of 15X28 that contain only the crop of location 523,724 ? (Every iteration will be with different size and with different location). (In the next iteration i can get Bitmap with 500*500 , used size 123*243 and the location will be in 23*342 for eample) Every time i will want a new Bitmap with only a crop of the used part of the old Bitmap. How can i do it?

          1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, the one way with decent performance goes like this: - create a new bitmap with appropriate size; - call CreateGraphics on it; - perform Graphics.DrawImage using an overload that lets you choose part of a source image - if necessary, save the bitmap in the format you like - Dispose of the objects you no longer need. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            B Offline
            B Offline
            bonzaiholding
            wrote on last edited by
            #5

            There is no source image, every time i will get Bitmap with another size and i want to cut the unused part, lets say that i will get Bitmap with the size 500X500 but there is only one small rectangle size 10X15 that locate in Point 124,254 in the orignal Bitmap and i want to get new Bitmap with the size 10X15 that contain the pixels from location Rectangle ( Location(124,254), Size(10X15)) but the only thing that i have is the bitmap. If i had the Rectangle of the used parts of the bitmap ,for example, i could just Clone the bitmap with this Rectangle, but how can i get this Rectangle?

            L 1 Reply Last reply
            0
            • B bonzaiholding

              There is no source image, every time i will get Bitmap with another size and i want to cut the unused part, lets say that i will get Bitmap with the size 500X500 but there is only one small rectangle size 10X15 that locate in Point 124,254 in the orignal Bitmap and i want to get new Bitmap with the size 10X15 that contain the pixels from location Rectangle ( Location(124,254), Size(10X15)) but the only thing that i have is the bitmap. If i had the Rectangle of the used parts of the bitmap ,for example, i could just Clone the bitmap with this Rectangle, but how can i get this Rectangle?

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              :confused: the image you have IS the source image, the bitmap you draw into is the destination image. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


              J 1 Reply Last reply
              0
              • B bonzaiholding

                If i want to crop a rectangle of the used part of a bitmap, what is the easiest way to do it?

                M Offline
                M Offline
                Moreno Airoldi
                wrote on last edited by
                #7

                If I understand correctly, your problem is locating the image you want to crop inside the bitmap. You said that it will always be a rectangle, and you talk about "empty" and "full" or "used" and "unused" pixels. This simplifies your problem a lot! You should be aware that image recognition is a rather complex field, and without these two assumptions you would have to go for complex solutions. Now, if you know the characteristics of the "empty" or "unused" pixels, you can recognize them. Fox example, they might be all of the same color (white ? black ?). So, starting from the corners of the image you can detect the first "used" pixels and define your cropping rectangle. How you do it is strongly dependant on the inner image characteristics. For example, if ALL of the inner image's pixels are different from the "unused" ones (different color, in my example), then you can walk along the diagonals of your bitmap to find the cropping area and then define it by walking along its edges if necessary, and so on. Hope this gives you some ideas. :)

                2+2=5 for very large amounts of 2 (always loved that one hehe!)

                1 Reply Last reply
                0
                • L Luc Pattyn

                  :confused: the image you have IS the source image, the bitmap you draw into is the destination image. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                  J Offline
                  J Offline
                  jasonmp
                  wrote on last edited by
                  #8

                  I like you, you're such a nice, humble person. :)

                  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