How can i crop only the used part of a Bitmap?
-
If i want to crop a rectangle of the used part of a bitmap, what is the easiest way to do it?
-
If i want to crop a rectangle of the used part of a bitmap, what is the easiest way to do it?
-
If i want to crop a rectangle of the used part of a bitmap, what is the easiest way to do it?
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.
-
Just create a new bitmap, get the pixel from original bitmap and set the pixel to new bitmap using GetPixel and SetPixel.
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?
-
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.
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?
-
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?
: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.
-
If i want to crop a rectangle of the used part of a bitmap, what is the easiest way to do it?
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!)
-
: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.