2

I am not a C# developer (embedded developer with C) but I have been forced to do a project which requires me to add a barcode to a PNG and print it,

if the resolution of the picture is high, the picture will be printed huge, covering a couple of labels (the printer works in 2 labels per row and 7 columns for each print command, a total of 14 labels)

now when I resize it, the picture will fit, however, the Quality will drop dramatically, and it's unreadable

this is the code I am using (The code is from Stackoverflow somewhere)

static Bitmap ScaleImage(Bitmap bmp, int maxWidth, int maxHeight)
{
    var ratioX = (double)maxWidth / bmp.Width;
    var ratioY = (double)maxHeight / bmp.Height;
    var ratio = Math.Min(ratioX, ratioY);

    var newWidth = (int)(bmp.Width * ratio);
    var newHeight = (int)(bmp.Height * ratio);

    var newImage = new Bitmap(newWidth, newHeight);

    using (var graphics = Graphics.FromImage(newImage))
        graphics.DrawImage(bmp, 0, 0, newWidth, newHeight);

    return newImage;
}

private void button1_Click(object sender, EventArgs e)
{
    var image = new Bitmap("C:\\Users\\gilani\\Desktop\\BarCodeCreator\\sad2.bmp");
    pictureBox1.Image = ScaleImage(image, 100, 80); 
}
private void button2_Click(object sender, EventArgs e)
{
    PrintDialog print = new PrintDialog();
    PrintDocument doc = new PrintDocument();
    doc.PrintPage += Doc_PrintPage;
    print.Document = doc;

    if (print.ShowDialog(this) == DialogResult.OK)
    {
        doc.Print();
    }
}

private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
    int start_right = 32;
    int start_Top = 0;
    Bitmap bitmp = new Bitmap(pictureBox1.Image), pictureBox1.Image.Height));
    e.Graphics.DrawImage(bitmp, start_right, start_Top);
    bitmp.Dispose();
}

I need a way to preserve the Resolution/ Quality but print in much smaller size (probably its caled scale) I might even be going the whole road wrong, please inform me if I need to take another way

2
  • 1
    IMHO "without" will be impossible, "with minimal loss" is doable - every scaling (if not a vector graphic) will result in a loss of quality Commented Jul 19, 2023 at 12:30
  • please check the method Doc_PrintPage there was an error, and my edit made it worse? I believe Commented Jul 19, 2023 at 12:33

2 Answers 2

1

The easiest thing to try is to use a more sophisticated interpolation mode. HighQualityBicubic should yield best results.

Change your code to:

using (var graphics = Graphics.FromImage(newImage)) 
{
    graphics.SetInterpolationMode(InterpolationMode.HighQualityBicubic);
    graphics.DrawImage(bmp, 0, 0, newWidth, newHeight);
}

You can find more information here.

0

I would try using .SetResolution to set the dpi to a higher value.

When displaying an image on the screen we normally want to map image pixels to screen pixels 1:1. But printers does not really have "pixles" at all, so you can configure how large a image pixel should be on the paper by setting the dpi. Images often have 96dpi since that should result in 1:1 mapping on a screen, but printers often have dpi of 200-300.

You might also need to adjust things like PageSettings.PrinterResolution on your printer document to get the best possible resolution from your printer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.