Resizing creates "border"

Jul 27, 2010 at 12:16 PM
First of all - this is a great piece of code - I really like it a lot. I have one small issue with it though - whenever I use it to resize an image I get a "border" on two sides of the image - usually some shade of grey - and I can't seem to figure out why - any suggestions?
Sep 22, 2010 at 7:16 AM

Did you ever get to the bottom of this as, I'm having the same issue that you mentioned.

Sep 22, 2010 at 7:22 AM

No sorry - I made my own handler instead :)

Developer
Jun 24, 2011 at 1:53 PM

Was this border a slight brightening (like 50%) of the outer edge of pixels around the entire image? If so, how did you avoid this GDI bug?

Jul 1, 2011 at 4:43 PM

I have a fix for it. I'm trying to get the lead of the project to apply patches... if that doesn't happen, I will be forking the project onto GitHub.

It will have the proper resizing fixes as well as plenty of other features (such as secure URLs).

If you cannot wait, here is my updated function that fixes the issue (in NativeImageTool.cs):

		public virtual Bitmap Resize(Bitmap source, int? width, int? height, bool preservePerspective)
		{
			double newWidth = width.HasValue ? Convert.ToDouble(width.Value) : 0;
			double newHeight = height.HasValue ? Convert.ToDouble(height.Value) : 0;
 
			if (preservePerspective)
			{
				if (newWidth > 0 && newHeight <= 0)
				{
					newHeight = (newWidth/source.Width)*source.Height;
				}
				else if (newHeight > 0 && newWidth <= 0)
				{
					newWidth = (newHeight/source.Height)*source.Width;
				}
			}
 
			if (newHeight <= 0)
			{
				newHeight = 1;
			}
 
			if (newWidth <= 0)
			{
				newWidth = 1;
			}
 
			var bitmap = new Bitmap((int)newWidth, (int)newHeight);
			
			using (Graphics graphics = Graphics.FromImage(bitmap))
			{
				graphics.CompositingMode = CompositingMode.SourceOver;
				graphics.CompositingQuality = CompositingQuality.HighQuality;
				graphics.SmoothingMode = SmoothingMode.HighQuality;
				graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
				graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
 
				using (var imageAttr = new ImageAttributes())
				{
					imageAttr.SetWrapMode(WrapMode.TileFlipXY);
 
					int stretchWidth = (int)Math.Floor((double)newWidth);
					int stretchHeight = (int)Math.Floor((double)newHeight);
 
					graphics.DrawImage(
						source,
						new Rectangle(00, stretchWidth, stretchHeight),
						0,
						0,
						source.Width,
						source.Height,
						GraphicsUnit.Pixel,
						imageAttr);
 
					return bitmap;
				}
			}
		}
Developer
Jul 20, 2011 at 6:06 PM

Ok, just wanted to make sure I wasn't missing something - I fixed that bug 3 years back in the http://imageresizing.net project.

By the way, Jaben, great job with YAF :) I worked with it several years ago and was pleasantly surprised!

On a different topic...

Right now I'm trying to get the multitude of authors of various 'image resizing' libraries for .NET to start working together. At last count, there were 12 (8 of which were open-source), including mine, http://imageresizing.net/.

I'm openly inviting everyone to download the source code at http://imageresizing.net, make changes, and submit them. I also have a Git repository for those who are interested. Just e-mail me, I'll set you up.

The imageresizing.net Git repository has accumulated over 500 commits over the last 2 years (the first 2 years I used .zip files). The large number of users has helped refine the library quickly, and define its goals well. It's very mature - time-tested, traffic-tested, and unit-tested :)

 

I'd like to talk to Ebsen about collaborating or merging our projects, as I probably have more time for maintaining them. My personal e-mail is nathanael.jones@gmail.com, and my ticket queue is support@imageresizing.net. Use the latter if possible, as I lose fewer e-mails that way. I don't have Ebsen's e-mail address, so I have to hope he'll contact me.

The WebImageResizer project is one of the best of the 12 projects, although it doesn't avoid all of the image resizing pitfalls: http://nathanaeljones.com/163/20-image-resizing-pitfalls/

WebImageResizer is also better than the http://imageresizing.net project when it comes to encoding size, due to the optional WPF and FreeImage encoders. I'd like to add those as plugins to the http://imageresizing.net project, and perhaps even extend support for performing the resizing in FreeImage as well, to speed up certain frequently used code paths. I greatly appreciate Ebsen's experience here, and I would like to get his advice.

Both libraries have their strengths and weaknesses, and I'd like to see their strengths combined. And, I'd like to see more collaboration. Feedback is valuable, and the more eyes that look at our code, the happier I am.

 

 

 

Developer
Aug 3, 2011 at 12:24 AM

@Jaben - Would you be interested in contributing your patches to the project? I'm a developer on the project now, so I can help get bugs fixed and new versions released.