Click here to Skip to main content
15,912,493 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i have an image and i m using LinearGradientBrush for write on image but it change image quality to low quality and blear on save the image
what should i do it don't change quality on save?
my save code
C#
bp = new Bitmap(pictureBox1.Image, width, height);
Graphics g = Graphics.FromImage(bp);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.CompositingQuality = CompositingQuality.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
bp.Save(save.FileName);

and these are pictures of problem
1-original image
Original Image[^]
2-and this is the image wrote on it with LinearGradientBruh
wrote on image with LinearGradientBruh [^]
Best Regards
Posted
Comments
BillWoodruff 16-Nov-14 17:04pm    
What font are you using; is it OpenType ?

As you probably know, jpeg is using a lossy compression method. Thus it is up to you to specify comression quality. Still it is not straighforward. See here: http://msdn.microsoft.com/en-us/library/bb882583(v=vs.110).aspx[^]. I have read that it is 75% by default, but I haven't found any reliable reference about.
 
Share this answer
 
v2
Comments
Avenger1 16-Nov-14 16:53pm    
when i using LinearGradientBruh for write on image, this code in msdn can't work
and it show picture again like that and problem isn't just with jpeg extension
Zoltán Zörgő 16-Nov-14 17:17pm    
I don't know why can't it work. I have just tried it out in LinqPad. Your original picture is 73kB. Without specifying quality, the saved image was 43kB, with 100% quality it is 184kB. And I don't see any degradation visually either. I have tested it with SolidBrush and LinearGradientBrush also.


var bp = new Bitmap(@"d:\temp\sj0.jpg");
Graphics g = Graphics.FromImage(bp);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.CompositingQuality = CompositingQuality.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
//var brush = new SolidBrush(Color.Black);
LinearGradientBrush linGrBrush = new LinearGradientBrush(
new Point(0, 10),
new Point(200, 10),
Color.FromArgb(255, 255, 0, 0), // Opaque red
Color.FromArgb(255, 0, 0, 255)); // Opaque blue

g.DrawString("Scarlett Johansson", new Font("Arial", 16), linGrBrush, new PointF(0.0F, 0.0F));
ImageCodecInfo jgpEncoder = ImageCodecInfo.GetImageDecoders().First(c => c.FormatID == ImageFormat.Jpeg.Guid);
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
var myEncoderParameters = new EncoderParameters(1);
var myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
bp.Save(@"d:\temp\sj.jpg", jgpEncoder, myEncoderParameters);
"How to: Use Antialiasing with Text" [^].

"When the Graphics.SmoothingMode property is specified by using the SmoothingMode enumeration, it does not affect text. To set the text rendering quality, use the Graphics.TextRenderingHint property and the TextRenderingHint enumeration." [^].

Your code does not show any rendering of Text on the image.

Is your source picture a .jpg ? Or ????

What font are you using; is it OpenType ?

Why would you use a LinearGradientBrush to do Text ? Your second picture shows no evidence of any gradient being applied to the Text.

What are you doing in the Paint Event ?

I think you should take your source image turn it into a .png image, then draw the Text on it, and save it as .png: [^].
 
Share this answer
 
v2
Comments
Avenger1 16-Nov-14 23:51pm    
thanks for your good answer, i found my problem but i don't know how can i fix it
in my save code if you see i wrote "width" and "height" these are my image size and when i saving LinearGradientBrush with them i have this problem else no
and when i save without "width" and "height" but using LinearGradientBrush everything is ok
what should i do to it save with those "width" and "height"?
BillWoodruff 17-Nov-14 0:15am    
Well, I'm glad you found some value in my response, since someone down-voted it :)

You are supplying values for 'width and 'height in your constructor for a new Bitmap; I interpret that to mean you are re-sizing the image.

If your re-size dimensions don't match the proportion of the original image's width/height (aspect ratio), then that's going to affect (scale) the image's appearance, but that should not affect the text you draw on the image.

Hard to say more without seeing how you are drawing the Text.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900