C
C#2w ago
Silme94

BAD IMAGE QUALITY (Bitmap)

Making a functionnality to add image to a page, but when i save it the quality is just horrible. It feel like on screen the image difference are the same, but there is very a big quality difference. Is there a way to make it look way better? Code :
using (Bitmap bmp = new Bitmap(pageImage.Width, pageImage.Height))
{
pageImage.DrawToBitmap(bmp, new Rectangle(0, 0, pageImage.Width, pageImage.Height));
bmp.Save(originalLocation);
}
using (Bitmap bmp = new Bitmap(pageImage.Width, pageImage.Height))
{
pageImage.DrawToBitmap(bmp, new Rectangle(0, 0, pageImage.Width, pageImage.Height));
bmp.Save(originalLocation);
}
No description
No description
7 Replies
Sehra
Sehra2w ago
some more context would help. what is pageImage?
Silme94
Silme94OP2w ago
pageImage is the PictureBox
Silme94
Silme94OP2w ago
No description
Sehra
Sehra2w ago
maybe need to tweak the interpolation mode, or scale it properly when switching from 96 dpi screen to 300 dpi pdf
Salman
Salman2w ago
maybe try this :
RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.HighQuality);
RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.HighQuality);
it helped me to make the image quality better back then
Lyrcaxis
Lyrcaxis2w ago
For reference I stopped having to deal with Bitmap in favour of storing everything as bytes:
public static byte[] GetGraphic(string path) {
using var m = new MemoryStream();
var image = System.Drawing.Image.FromFile(path);
image.Save(m, image.RawFormat);
return m.ToArray();
}
public static byte[] GetGraphic(string path) {
using var m = new MemoryStream();
var image = System.Drawing.Image.FromFile(path);
image.Save(m, image.RawFormat);
return m.ToArray();
}
So something like: ImageBytes = Utils.GetGraphic(path); and rendering via <Image Source="{Binding ImageBytes}"/> ok now I see you're querying about saving it, not loading it. You can do the same:
using var m = new MemoryStream();
image.Save(memoryStream, ImageFormat.Png);
return m.ToArray();
using var m = new MemoryStream();
image.Save(memoryStream, ImageFormat.Png);
return m.ToArray();
Then System.IO.File.WriteAllBytes.
Silme94
Silme94OP2w ago
@Salman @Sehra still the same quality...
using (MemoryStream memoryStream = new MemoryStream())
{
Image controlImage = new Bitmap(pageImage.Width, pageImage.Height);

using (Graphics graphics = Graphics.FromImage(controlImage))
{
graphics.Clear(Color.White);
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

pageImage.DrawToBitmap((Bitmap)controlImage, new Rectangle(0, 0, pageImage.Width, pageImage.Height));
}

controlImage.Save(memoryStream, ImageFormat.Png);
File.WriteAllBytes(originalLocation, memoryStream.ToArray());
}
using (MemoryStream memoryStream = new MemoryStream())
{
Image controlImage = new Bitmap(pageImage.Width, pageImage.Height);

using (Graphics graphics = Graphics.FromImage(controlImage))
{
graphics.Clear(Color.White);
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

pageImage.DrawToBitmap((Bitmap)controlImage, new Rectangle(0, 0, pageImage.Width, pageImage.Height));
}

controlImage.Save(memoryStream, ImageFormat.Png);
File.WriteAllBytes(originalLocation, memoryStream.ToArray());
}

Did you find this page helpful?