【问题标题】:save an image as a bitmap without losing quality将图像保存为位图而不损失质量
【发布时间】:2012-07-26 19:20:25
【问题描述】:

我遇到了打印质量问题,我在此链接中进行了描述: enter link description here

我尝试了许多不同的解决方案来帮助其他有类似问题的人,但它们对我不起作用,因为我有一个将图像保存为位图的新问题(低质量)

最后我决定问我当前的问题,因为正如你在上面的链接中看到的,我的问题是在将图像保存到系统(96dpi)并恢复它之后开始的。但我没有办法,所以我正在寻找一种方法,可以在不损失质量的情况下保存图像(具有从图形中提取的像素)。

提前感谢

【问题讨论】:

  • 不完全理解你的问题,但我看到两件事不太适合,那就是“96dpi”和“打印”。您至少需要 300dpi 才能获得不错的打印效果。因此,我认为答案是您只需要更高分辨率的位图。

标签: c# bitmap graphic


【解决方案1】:

虽然 96 dpi 适用于屏幕显示,但不适用于打印。对于打印,您至少需要 300 dpi 才能使其看起来清晰。

出于好奇,我创建了一个在 600 dpi 位图上打印文本的 C# 控制台应用程序。 我想出了这个:

class Program
{
    public static void Main(string[] args)
    {
        const int dotsPerInch = 600;    // define the quality in DPI
        const double widthInInch = 6;   // width of the bitmap in INCH
        const double heightInInch = 1;  // height of the bitmap in INCH

        using (Bitmap bitmap = new Bitmap((int)(widthInInch * dotsPerInch), (int)(heightInInch * dotsPerInch)))
        {
            bitmap.SetResolution(dotsPerInch, dotsPerInch);

            using (Font font = new Font(FontFamily.GenericSansSerif, 0.8f, FontStyle.Bold, GraphicsUnit.Inch))
            using (Brush brush = Brushes.Black)
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.Clear(Color.White);
                graphics.DrawString("Wow, I can C#", font, brush, 2, 2);
            }
            // Save the bitmap
            bitmap.Save("n:\\test.bmp");
            // Print the bitmap
            using (PrintDocument printDocument = new PrintDocument())
            {
                printDocument.PrintPage += (object sender, PrintPageEventArgs e) =>
                {
                    e.Graphics.DrawImage(bitmap, 0, 0);
                };
                printDocument.Print();
            }
        }
    }
}

这是打印出来的结果

【讨论】:

  • 另请注意,打印时绝不应消除文本和线条的锯齿。否则灰色边缘在纸上会变成模糊边缘。
  • @WoutwrH:我的项目流程和你的例子一模一样。我刚刚添加了 SetResolution,它提高了我的文本质量,但放大了我的文本!我该如何解决?
  • @Joey : 啊哈,谢谢你的指导 :)
  • @Shima,如果没有看到任何代码行,这很难说。您可以修改您的问题并添加一些代码。另请注意,在我的示例中,创建了 3600 x 600 像素的位图,因此如果以全分辨率打开位图,文本确实会非常大。但这是正常的,因为我们有一个 600dpi 的分辨率,您可以在 96dpi 的屏幕上打开。
  • 也不要混淆像英寸、像素、点这样的单位......在一个单位中计算所有内容并在需要时执行计算(就像我对new Bitmap((int)(widthInInch * dotsPerInch), ...所做的那样)。还使用相同单位的值设置字体大小:f.e. Font(FontFamily.GenericSansSerif, 0.8f, FontStyle.Bold, GraphicsUnit.Inch) 将大小设置为 0.8 英寸。
猜你喜欢
  • 1970-01-01
  • 2014-03-22
  • 1970-01-01
  • 1970-01-01
  • 2012-10-08
  • 2015-01-24
  • 2014-10-06
  • 1970-01-01
相关资源
最近更新 更多