Last post Apr 01, 2010 11:24 PM by vcsjones
Mar 21, 2010 11:50 AM|ellekz|LINK
I'm new to .NET and I wanted to draw a simple, vertical, 1px line on an image. However, the line always gets "smoothed" even though I tried everything in System.Drawing.Graphics to make it stop.
Here's an image (zoomed in) of what I mean:
Here's my code:
Mar 21, 2010 06:18 PM|vcsjones|LINK
I don't think the issue is that it is aliasing. Infact the default behavior is it
won't alias. This code does what you want it to do:
Bitmap bmp = new Bitmap(500,500);
Graphics g = Graphics.FromImage(bmp);
g.DrawLine(new Pen(Color.Red, 5f), 10, 10, 10, 480);
What I think you are seeing is possibly because you are saving it in a lossy compression like JPEG which causes it to appear to be aliased. Were you saving your image as a JPEG?
Mar 22, 2010 11:18 AM|ellekz|LINK
Wow, you're right. I can't believe I didn't think of that. I've been using the usual JPEG-lib before and also Photoshop's "Save as JPEG" but I have never ever seen a JPEG compression this bad. It works fine as BMP and PNG but even when setting the quality
to "100" the .NET-implementation is really really bad. Is there any way to improve this or am I doing something wrong?
uint compression = 100;
ImageCodecInfo imageEncoderJpeg = null;
ImageCodecInfo encoders = ImageCodecInfo.GetImageEncoders();
foreach(ImageCodecInfo info in encoders)
if(info.FormatID == ImageFormat.Jpeg.Guid)
imageEncoderJpeg = info;
if(imageEncoderJpeg == null)
EncoderParameters parameters = new EncoderParameters(2);
parameters.Param = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)compression);
parameters.Param = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)compression);
image.Save(path, imageEncoderJpeg, parameters);
Mar 22, 2010 10:55 PM|vcsjones|LINK
As far as I can tell, that is the way to produce the best quality images in JPEG using .NET. If this isn't suitable, I'd recommend another image format, or saving them as PNG/BMP and using a 3rd party tool to process them into JPEGs.
Mar 29, 2010 04:24 PM|ellekz|LINK
I found out what the problem is. NET uses standard subsampling (4:2:2) when saving JPEGs, while every one else (e.g. Photoshop) uses no subsampling (4:4:4) when saving JPEG with a quality over 50%.
So, all I'd need to do is disable subsampling in NET - but that doesn't seem to be possible (maybe it's possible with Encoder.ChrominanceTable or Encoder.LuminanceTable but I have no idea how to do that and the documentation isn't helpful). I just spend
searching the Internet with all possible keywords but it seems like only a handful of people wanted to do that and no one really succeeded (except one guy, but he didn't post an answer).
I'm hoping someone here might have a clue...
Mar 30, 2010 08:53 AM|vcsjones|LINK
Interesting discovery... I'll play around with it and see if I can find out. Thanks for the tip!
Mar 30, 2010 01:09 PM|ellekz|LINK
Wow, thank you for your effort trying to solve this! It'd be pretty cool if you happen to find a solution :)
Apr 01, 2010 11:24 PM|vcsjones|LINK
Most solutions I was able to find were wrapping 3rd party libraries. There doesn't appear to be any known way, that I could find, to get the .NET Framework to do this itself.