tag:blogger.com,1999:blog-1009430530424913741.post4479440354476054285..comments2024-03-21T04:25:29.927+00:00Comments on ITDevSpace - Development Tips & Tricks: C# Crop white space from around the imageUnknownnoreply@blogger.comBlogger10125tag:blogger.com,1999:blog-1009430530424913741.post-49676438238197654272021-06-30T10:37:20.444+01:002021-06-30T10:37:20.444+01:00the most effective way to invest your savings is
...the most effective way to invest your savings is <br /><br /><a href="https://aquaproperties.com/villas-for-rent-in-dubai" rel="nofollow">properties for rent in dubai</a><br /><a href="https://aquaproperties.com/property-for-sale-in-dubai" rel="nofollow">Properties For Sale In Dubai</a><br /><a href="https://aquaproperties.com/apartments-for-sale-in-dubai" rel="nofollow">property for sale in downtown dubai</a><br /><a href="https://aquaproperties.com/community/palm-jumeirah" rel="nofollow">property for rent in palm jumeirah</a><br /><a href="https://aquaproperties.com/apartments-for-rent-in-dubai-the-crescent-(-palm-jumeirah)" rel="nofollow">apartments for rent in palm jumeirah</a><br /><a href="https://aquaproperties.com/community/downtown-dubai" rel="nofollow">property for rent in downtown dubai</a><br /><a href="https://aquaproperties.com/villas-for-rent-in-dubai" rel="nofollow">villas for rent in dubai</a><br /><a href="https://aquaproperties.com/townhouses-for-sale-in-dubai" rel="nofollow">villas for sale in dubai</a><br /><a href="https://aquaproperties.com/apartments-for-sale-in-dubai" rel="nofollow">property for sale in burj khalifa dubai</a><br /><a href="https://aquaproperties.com/apartments-for-rent-in-dubai-the-crescent-(-palm-jumeirah)" rel="nofollow">apartments for rent in palm jumeirah</a><br /><a href="https://aquaproperties.com/apartments-for-sale-in-dubai" rel="nofollow">apartments for sale in burj khalifa dubai</a><br /><a href="https://aquaproperties.com/property-search?a=Dubai+Hills+Estate&type=residential&f=sale&c=&minprice=&maxprice=&minbed=&maxbed=&r=" rel="nofollow">Apartments for sale in Dubai Hills Estate</a>STEVEhttps://www.blogger.com/profile/16604186689381152095noreply@blogger.comtag:blogger.com,1999:blog-1009430530424913741.post-69958012899977825162021-03-18T05:49:54.709+00:002021-03-18T05:49:54.709+00:00You have given great content here. I am glad to di...You have given great content here. I am glad to discover this post as I found lots of valuable data in your article. Thanks for sharing an article like this. <a href="https://technewspress.com/remove-background-from-image-3-simple-ways-to-get-rid-of-backgrounds-in-your-photos/" rel="nofollow">Remove Background From Image</a>Laura Bushhttps://www.blogger.com/profile/14063697739786180181noreply@blogger.comtag:blogger.com,1999:blog-1009430530424913741.post-81246112849309668062018-09-25T08:59:26.039+01:002018-09-25T08:59:26.039+01:00It works good guys.
public void TrimImage() {
...<br />It works good guys.<br /><br /><br />public void TrimImage() {<br /> int threshhold = 250;<br /><br /><br /> int topOffset = 0;<br /> int bottomOffset = 0;<br /> int leftOffset = 0;<br /> int rightOffset = 0;<br /> Bitmap img = new Bitmap(@"e:\Temp\Trim_Blank_Image.png");<br /><br /><br /> bool foundColor = false;<br /> // Get left bounds to crop<br /> for (int x = 1; x < img.Width && foundColor == false; x++)<br /> {<br /> for (int y = 1; y < img.Height && foundColor == false; y++)<br /> {<br /> Color color = img.GetPixel(x, y);<br /> if (color.R < threshhold || color.G < threshhold || color.B < threshhold)<br /> foundColor = true;<br /> }<br /> leftOffset += 1;<br /> }<br /><br /><br /> foundColor = false;<br /> // Get top bounds to crop<br /> for (int y = 1; y < img.Height && foundColor == false; y++)<br /> {<br /> for (int x = 1; x < img.Width && foundColor == false; x++)<br /> {<br /> Color color = img.GetPixel(x, y);<br /> if (color.R < threshhold || color.G < threshhold || color.B < threshhold)<br /> foundColor = true;<br /> }<br /> topOffset += 1;<br /> }<br /><br /><br /> foundColor = false;<br /> // Get right bounds to crop<br /> for (int x = img.Width - 1; x >= 1 && foundColor == false; x--)<br /> {<br /> for (int y = 1; y < img.Height && foundColor == false; y++)<br /> {<br /> Color color = img.GetPixel(x, y);<br /> if (color.R < threshhold || color.G < threshhold || color.B < threshhold)<br /> foundColor = true;<br /> }<br /> rightOffset += 1;<br /> }<br /><br /><br /> foundColor = false;<br /> // Get bottom bounds to crop<br /> for (int y = img.Height - 1; y >= 1 && foundColor == false; y--)<br /> {<br /> for (int x = 1; x < img.Width && foundColor == false; x++)<br /> {<br /> Color color = img.GetPixel(x, y);<br /> if (color.R < threshhold || color.G < threshhold || color.B < threshhold)<br /> foundColor = true;<br /> }<br /> bottomOffset += 1;<br /> }<br /><br /><br /> // Create a new image set to the size of the original minus the white space<br /> //Bitmap newImg = new Bitmap(img.Width - leftOffset - rightOffset, img.Height - topOffset - bottomOffset);<br /><br /> Bitmap croppedBitmap = new Bitmap(img);<br /> croppedBitmap = croppedBitmap.Clone(<br /> new Rectangle(leftOffset - 3, topOffset - 3, img.Width - leftOffset - rightOffset + 6, img.Height - topOffset - bottomOffset + 6),<br /> System.Drawing.Imaging.PixelFormat.DontCare);<br /><br /><br /> // Get a graphics object for the new bitmap, and draw the original bitmap onto it, offsetting it do remove the whitespace<br /> //Graphics g = Graphics.FromImage(croppedBitmap);<br /> //g.DrawImage(img, 1 - leftOffset, 1 - rightOffset);<br /> croppedBitmap.Save(@"e:\Temp\Trim_Blank_Image-crop.png", ImageFormat.Png);<br />}Đỗ Thành Trunghttps://www.blogger.com/profile/04128736326491010233noreply@blogger.comtag:blogger.com,1999:blog-1009430530424913741.post-20146257722742343352012-10-03T17:14:16.022+01:002012-10-03T17:14:16.022+01:00Ok! I understand now...
The matter was that I foun...Ok! I understand now...<br />The matter was that I found your article on Google and I was looking especially for "C# Crop white space from around the image" (there was a discussion with my colleague) and I saw it. But I saw that it wasn't the answer to my question so I decided to write to you and explain my code.<br />Tedebushttps://www.blogger.com/profile/06861414130589798965noreply@blogger.comtag:blogger.com,1999:blog-1009430530424913741.post-73122860764853106502012-10-03T16:45:47.850+01:002012-10-03T16:45:47.850+01:00And thanks for positing your code!And thanks for positing your code!madeinsteinhttps://www.blogger.com/profile/04743131327324822229noreply@blogger.comtag:blogger.com,1999:blog-1009430530424913741.post-2176032706670774632012-10-03T16:41:13.672+01:002012-10-03T16:41:13.672+01:00Hi, just to explain, the purpose of this code was ...Hi, just to explain, the purpose of this code was to remove unnecessary white space from around the image to make the image boundaries (square) close to the actual image so I can send that image in the email. I've used it after creating image from html template described here: <a href="http://www.itdevspace.com/2012/04/c-convert-html-page-to-image.html" rel="nofollow">C# Convert html page to image</a> You're trying to go a bit further to make the remaining space outside the actual image transparent. And yes I've uploaded an extra image to show how it works for non square images. madeinsteinhttps://www.blogger.com/profile/04743131327324822229noreply@blogger.comtag:blogger.com,1999:blog-1009430530424913741.post-48934526264916177042012-10-03T16:05:32.075+01:002012-10-03T16:05:32.075+01:00PS "tollerance" is not used, I posted a ...PS "tollerance" is not used, I posted a piece of code that I used somewhere else and I forgot to delete it.Tedebushttps://www.blogger.com/profile/06861414130589798965noreply@blogger.comtag:blogger.com,1999:blog-1009430530424913741.post-17838785498512142662012-10-03T16:03:21.576+01:002012-10-03T16:03:21.576+01:00Hi!
I tried your code and I saw that irregular ima...Hi!<br />I tried your code and I saw that irregular images still contain white areas (like you show on your article, now).<br />If your idea is to create a PNG (that has alpha information inside) you may prefer a different approach: bitmap reconstruction based on transparent color substitution on external white areas and, then, image crop.<br />This may be an example of code that does it<br />(please note that it crop not only white color, but any bounding color)<br /><br /> public static System.Drawing.Bitmap ClearBitmapBorders(System.Drawing.Bitmap bitmap, int tollerance)<br /> {<br /> Color pixelColor;<br /> Color referenceColor = bitmap.GetPixel(0, 0);<br /> int X1 = bitmap.Width;<br /> int Y1 = bitmap.Height;<br /> int X2 = 0;<br /> int Y2 = 0;<br /> int row, column = 0;<br /> for (row = 0; row < bitmap.Height; row++)<br /> {<br /> for (column = 0; column < bitmap.Width; column++)<br /> {<br /> pixelColor = bitmap.GetPixel(column, row);<br /> if ((pixelColor.ToArgb() << 8 == referenceColor.ToArgb() << 8))<br /> { bitmap.SetPixel(column, row, Color.Transparent); }<br /> else<br /> {<br /> X1 = column < X1 ? column : X1;<br /> Y1 = row < Y1 ? row : Y1;<br /> break;<br /> }<br /> }<br /> for (column = bitmap.Width - 1; column >= 0; column--)<br /> {<br /> pixelColor = bitmap.GetPixel(column, row);<br /> if ((pixelColor.ToArgb() << 8 == referenceColor.ToArgb() << 8))<br /> { bitmap.SetPixel(column, row, Color.Transparent); }<br /> else<br /> {<br /> X2 = column > X2 ? column : X2;<br /> System.Diagnostics.Debug.WriteLine(X2);<br /> Y2 = row > Y2 ? row : Y2;<br /> break;<br /> }<br /> }<br /> }<br /> try<br /> {<br /> var target = new Bitmap(X2, Y2);<br /> using (Graphics g = Graphics.FromImage(target))<br /> {<br /> g.DrawImage(bitmap,<br /> new RectangleF(0, 0, X2, Y2),<br /> new RectangleF(X1, Y1, X2, Y2),<br /> GraphicsUnit.Pixel);<br /> }<br /> return target;<br /> }<br /> catch<br /> { return null; }<br /> }<br /><br />If you try it on your image you can see that now only red area will be kept and it remains white inside.<br /><br />I hope it should be usefulTedebushttps://www.blogger.com/profile/06861414130589798965noreply@blogger.comtag:blogger.com,1999:blog-1009430530424913741.post-6192652815167415002012-10-03T08:48:51.197+01:002012-10-03T08:48:51.197+01:00Hi Tedebus, this works fine for any shape. It find...Hi Tedebus, this works fine for any shape. It finds the first non-white pixel from top/bottom/left/right and crops the image based on this information.madeinsteinhttps://www.blogger.com/profile/04743131327324822229noreply@blogger.comtag:blogger.com,1999:blog-1009430530424913741.post-88355422770590048172012-10-02T11:40:00.836+01:002012-10-02T11:40:00.836+01:00I see that this procedure doesn't work if the ...I see that this procedure doesn't work if the image is not a rectangle, is it true? <br />So, why don't look only for the first not white pixel (from beginning of the bitmap) and the last one (from the end)?<br />From their coordinates you can simply calculate the new rectangle avoiding a lot of code!<br />Tedebusnoreply@blogger.com