Answered by:
White border on resized image

Question
-
User1329752403 posted
Hello everyone,
i have to resize some images. I use the following code to resize them (i have seen this code several times here in the forum).
The quality and size of the images is very good. But the problem is i always have a white border on the bottom site and on the right side of the image (width of 1px).
Does anyone have the same effect? Or does anybody sees the mistake in the code (has to be somewhere on line 81 and further)?
Thank you very much
Klaus
The code:
1 public static Bitmap CreateThumbnail(string lcFilename,int lnWidth, int lnHeight) 2 3 { 4 5 6 7 System.Drawing.Bitmap bmpOut = null; 8 9 try 10 11 { 12 13 Bitmap loBMP = new Bitmap(lcFilename); 14 15 ImageFormat loFormat = loBMP.RawFormat; 16 17 18 19 decimal lnRatio; 20 21 int lnNewWidth = 0; 22 23 int lnNewHeight = 0; 24 25 26 27 //*** If the image is smaller than a thumbnail just return it 28 29 if (loBMP.Width < lnWidth && loBMP.Height < lnHeight) 30 31 return loBMP; 32 33 34 35 36 37 if (loBMP.Width > loBMP.Height) 38 39 { 40 41 lnRatio = (decimal) lnWidth / loBMP.Width; 42 43 lnNewWidth = lnWidth; 44 45 decimal lnTemp = loBMP.Height * lnRatio; 46 47 lnNewHeight = (int)lnTemp; 48 49 } 50 51 else 52 53 { 54 55 lnRatio = (decimal) lnHeight / loBMP.Height; 56 57 lnNewHeight = lnHeight; 58 59 decimal lnTemp = loBMP.Width * lnRatio; 60 61 lnNewWidth = (int) lnTemp; 62 63 } 64 65 66 67 // System.Drawing.Image imgOut = 68 69 // loBMP.GetThumbnailImage(lnNewWidth,lnNewHeight, 70 71 // null,IntPtr.Zero); 72 73 74 75 // *** This code creates cleaner (though bigger) thumbnails and properly 76 77 // *** and handles GIF files better by generating a white background for 78 79 // *** transparent images (as opposed to black) 80 81 bmpOut = new Bitmap(lnNewWidth, lnNewHeight); 82 83 Graphics g = Graphics.FromImage(bmpOut); 84 85 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 86 87 g.FillRectangle(Brushes.White,0,0,lnNewWidth,lnNewHeight); 88 89 g.DrawImage(loBMP,0,0,lnNewWidth,lnNewHeight); 90 91 loBMP.Dispose(); 92 93 }catch{ 94 95 return null; 96 97 } 98 99 100 101 return bmpOut; 102 103 } 104
Wednesday, October 4, 2006 2:35 AM
Answers
-
User-1395392389 posted
I've tested a bit (I experienced your problems at the top-left edges, setting dimensions under 300px of width)...
-
Obviously, if you don't fill the canvas with white pixels (line 87) you have no problems;
-
If you don't set the interpolation mode, you have no problems
Probably...
-
the Algorithm which lies behind the HighQualityBicubic interpolation mode shrinks a bit or shades off the pixels at the edges - even using Photoshop you could notice similar behaviors.
Possible solutions:
-
remove line 87 (but you likely have seriuos reasons to keep it)
-
remove line 85 (but the image quality gets worse)
-
If a pixel isn't a life or death problem you could rewrite line 89 as...
g.DrawImage(loBMP,-1,-1,lnNewWidth+2,lnNewHeight+2);
Keep posting, I'm curious to know the real reason and an head-cutting solution to this.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 6, 2006 4:53 AM -
-
User1329752403 posted
If you convert the code of the first post to ASP.NET 2.0 than it works fine. [Yes]
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, December 3, 2007 5:49 AM
All replies
-
User-1395392389 posted
I've tested a bit (I experienced your problems at the top-left edges, setting dimensions under 300px of width)...
-
Obviously, if you don't fill the canvas with white pixels (line 87) you have no problems;
-
If you don't set the interpolation mode, you have no problems
Probably...
-
the Algorithm which lies behind the HighQualityBicubic interpolation mode shrinks a bit or shades off the pixels at the edges - even using Photoshop you could notice similar behaviors.
Possible solutions:
-
remove line 87 (but you likely have seriuos reasons to keep it)
-
remove line 85 (but the image quality gets worse)
-
If a pixel isn't a life or death problem you could rewrite line 89 as...
g.DrawImage(loBMP,-1,-1,lnNewWidth+2,lnNewHeight+2);
Keep posting, I'm curious to know the real reason and an head-cutting solution to this.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, October 6, 2006 4:53 AM -
-
User1239637383 posted
FAIL
I once killed a man over 1 pixel.
g.DrawImage(loBMP,-1,-1,lnNewWidth+1,lnNewHeight+1);
it worked for him - but your taking your chances
Friday, October 12, 2007 2:06 AM -
User1329752403 posted
If you convert the code of the first post to ASP.NET 2.0 than it works fine. [Yes]
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, December 3, 2007 5:49 AM -
User294545519 posted
I'm also trying to find a way of solving this problem and have yet to find a solution. I posted about it here:
http://forums.asp.net/t/1186976.aspx
As this thread now seems to be resolved can someone please explain to me how I can fix this.
Thanks.
Monday, December 3, 2007 5:56 AM -
User294545519 posted
I think Ive now found a solutions. ive changed the first line of code below into the second one and it works fine:
g.DrawImage(postedFile, 0, 0, width, height) g.DrawImage(postedFile, -1, -1, width + 1, height + 1)
Wednesday, December 5, 2007 11:42 AM -
User-921812012 posted
I realize this is an old thread, but hey...
I had a similar problem, and learned the following:
g.DrawImage(postedFile, -1, -1, width + 1, height + 1)
(this left a border on the right and bottom of my image)g.DrawImage(postedFile, -1, -1, width + 2, height + 2)
(no border, but lost a pixel, and I don't want to die over a lousy pixel)Dim
outRect As New RectangleF(-0.5, -0.5, width + 1, height + 1)
g.DrawImage(postedFile, outRect)
(worked flawlessly)Apparently, every InterpolationMode except 'NearestNeighbor' is calculated from the 'center' of the pixel. The RectangleF overload lets us compensate without cropping a pixel.
Tuesday, April 7, 2009 5:55 PM