使用程序化文本缩小打印质量图像的文件大小,而不会降低打印质量 [英] Reduce file size of a print quality image with programmatic text without losing print quality
问题描述
我需要加载图像文件,在其上设置一些文本,然后使用下面的代码将其保存为图像以进行打印。    
$
I need to load an image file, set some text on it, and save it as an image for printing using the code below.
private static void A4SizeDpi600() {
const int dotsPerInch = 600; // define the quality in DPI
const double widthInInch = 10; // width of the bitmap in INCH
const double heightInInch = 8; // height of the bitmap in INCH
using (Bitmap bitmap = new Bitmap(Image.FromFile(imageFilePath), (int)(widthInInch * dotsPerInch), (int)(heightInInch * dotsPerInch))) //10*600=6000, 8*600=4800
{
bitmap.SetResolution(dotsPerInch, dotsPerInch);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.DrawString("Some text here", new Font("Times New Roman", 0.3f, FontStyle.Regular, GraphicsUnit.Inch), Brushes.Black, 1400f, 2100f);
... //more text here
// Save the bitmap
bitmap.Save(imageBaseFilePath+ "CertDpi600.png");//file type can be any
}
}
问题是生成的图像太大(10MB),输入文件只有600K,超过10时间大小。我想知道是否有办法减少文件大小而不会失去打印质量。
生成的图像文件类型可以是任何类型(pdf, png, bmp,jpeg),只要它具有良好的打印质量和小文件大小。
我也对输入文件类型开放(任何图像文件类型,或pdf),只要它产生上述相同的要求。
The problem is that the resulting image is too big (10MB), the input file is only 600K, which is more than 10 times in size . I want to know if there are ways to reduce the file size without losing printing quality.
The resulting image file type can be any type (pdf, png, bmp, jpeg), as long as it has good printing quality and small file size.
I am open to the input file type too (either any image file type, or pdf), as long as it produces the same requirement above.
推荐答案
Hi Pingpong689,
Hi Pingpong689,
当然你可以通过以下完整演示来缩小图片:
Sure you can reduce the picture with the following complete demo:
public static string imageFilePath = @"C:\Users\v-baf\Desktop\Picon\8.jpg";
public static string imageBaseFilePath = @"D:\New folder (9)\";
private void button1_Click(object sender, EventArgs e)
{
A4SizeDpi600();
Image img = Image.FromFile(imageBaseFilePath + "CertDpi600.png");
SaveJpeg(imageBaseFilePath + "Result.png", img, 50);
}
private static void A4SizeDpi600()
{
const int dotsPerInch = 600; // define the quality in DPI
const double widthInInch = 10; // width of the bitmap in INCH
const double heightInInch = 8; // height of the bitmap in INCH
using (Bitmap bitmap = new Bitmap(Image.FromFile(imageFilePath), (int)(widthInInch * dotsPerInch), (int)(heightInInch * dotsPerInch))) //10*600=6000, 8*600=4800
{
bitmap.SetResolution(dotsPerInch, dotsPerInch);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.DrawString("Some text here", new Font("Times New Roman", 0.3f, FontStyle.Regular, GraphicsUnit.Inch), Brushes.Black, 1400f, 2100f);
//more text here
// Save the bitmap
bitmap.Save(imageBaseFilePath + "CertDpi600.png");//file type can be any
}
}
}
public static void SaveJpeg(string path, Image img, int quality)
{
if (quality < 0 || quality > 100)
throw new ArgumentOutOfRangeException("quality must be between 0 and 100.");
// Encoder parameter for image quality
EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
// JPEG image codec
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
img.Save(path, jpegCodec, encoderParams);
}
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
// Get image codecs for all image formats
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
// Find the correct image codec
for (int i = 0; i < codecs.Length; i++)
if (codecs[i].MimeType == mimeType)
return codecs[i];
return null;
}
结果截图:
请参阅
缩小图像大小C#[关闭]。
希望这会有所帮助!
最诚挚的问候,
Stanly
这篇关于使用程序化文本缩小打印质量图像的文件大小,而不会降低打印质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!