简体   繁体   中英

created image from code, looks pixelated when printing it

I am trying to print 40x40mm labels from a programmatically created image.

The label must have text on it, and a logo. Since the label is fairly small I am finding myself fiddling with how to do proper smooting, antialias and such.

I have tried multipl settings but I am not sure it's even the right way to go about it.

First I draw the container Bitmap:

private Bitmap DrawLabelCircle()
{
    var labelImage = new Bitmap(152, 152);

    using (Graphics gfx = Graphics.FromImage(labelImage))
    {
        var pen = new Pen(Color.Black, 1);
        gfx.SmoothingMode = SmoothingMode.AntiAlias;
        gfx.DrawEllipse(pen, 1, 1, 150, 150);
    }

    return labelImage;
}

Then I overlay different text snippets on that container Bitmap

private Bitmap DrawDistributorTextRectangle(string text)
{
    var bitmap = new Bitmap(113, 113);

    var rectangle = new Rectangle(0, 0, 110, 110);

    using (Graphics gfx = Graphics.FromImage(bitmap))
    {
        gfx.SmoothingMode = SmoothingMode.AntiAlias;
        gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
        gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;

        var font = new Font(FontFamily.GenericSansSerif, 5, FontStyle.Regular, GraphicsUnit.Point);
        var brush = new SolidBrush(Color.Black);

        gfx.TextRenderingHint = TextRenderingHint.AntiAlias;
        gfx.DrawString(text, font, brush, rectangle);
    }

    bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);

    return bitmap;
}

Overlay that text on the previous created Bitmap.

private Bitmap DistributorTextOverlay(Bitmap source, Bitmap overlay)
{
    var result = new Bitmap(source.Width, source.Height);

    var graphics = Graphics.FromImage(result);
    graphics.CompositingMode = CompositingMode.SourceOver;
    graphics.SmoothingMode = SmoothingMode.HighQuality;

    graphics.DrawImage(source, 0, 0);
    graphics.DrawImage(overlay, 120, 0);

    return result;
}

And the I save it.

var imageCodecInfo = ImageCodecInfo.GetImageEncoders().First(encoder => encoder.MimeType == "image/png");

var encoderInfo = new EncoderParameters() { Param = { [0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L) } };

image.SetResolution(203, 203);
image.Save("img.png", imageCodecInfo, encoderInfo);

The big challenge here is that the image I get is actually looking alright, all things considered.

But when I print it, it looks terrible pixelated.

I would really like to give some pointers for what settings I should apply to all these bitmaps before saving the final result, and what settings should apply for the final image I save.

I am by no means a .NET graphics expert so all help is much appreciated.

40mm is 1.5748 inches. So if you plan to print it at 300 dpi resolution, your bitmap should be 1.5748*300 = 472 pixels instead of 152.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM