C#图像内容修改和保存输入配置 [英] C# Image Content Modification and Save with input configurations

查看:65
本文介绍了C#图像内容修改和保存输入配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专家,下面是我的代码。我正在保存图像,并从DrawString向图像添加一些内容。我的问题是,一旦我保存图像,CMYK变成RGB,图像大小不同,图像dpi不同。就像改变了几个输入图像配置一样。



我只想保持相同的输入配置和质量,只修改内容。请帮帮我。



位图bmp =新位图(Image.FromFile(@D:\ input.jpg));



图形gr = Graphics.FromImage(bmp);





使用(Font myFont = new Font (Arial,42))

{

gr.DrawString(Hello!,myFont,Brushes.Green,new Point(2,2));

gr.DrawString(date.Text,myFont,Brushes.Black,new Point(700,798));

gr.DrawString(shape.Text,myFont,Brushes。 Black,new Point(700,908));

gr.DrawString(meas.Text,myFont,Brushes.Black,new Point(700,1031));

gr.DrawString(weight.Text,myFont,Brushes.Black,new Point(700,1160));

}

ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Qu ality;

EncoderParameters myEncoderParameters = new EncoderParameters(1);



EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder,300L);

myEncoderParameters.Param [0] = myEncoderParameter;

bmp.Save(@D:\ test.jpg,jgpEncoder,myEncoderParameters);

解决方案

这很奇怪,但首先,消除新的位图(图像)。使用

位图bmp =  new 位图(fileName); 



如果你真的想从一些抽象的 Image 实例初始化一个位图,当一个文件不可访问时,如果你还想改变大小和 PixelFormat ,您需要初始化具有所需属性的空位图,然后分别打开 Image 的实例,并绘制在您的位图上,使用 Graphics 实例,您将获得与您已经完全相同的方式(在您的代码中称为 gr )。在这种特殊情况下,您根本不需要它,因为您可以直接从文件打开位图,该文件应该保留像素格式,大小和所有其他属性。请参阅:

https ://msdn.microsoft.com/en-us/library/system.drawing.bitmap%28v=vs.110%29.aspx [ ^ ],

< a href =https://msdn.microsoft.com/en-us/library/0cbhe98f(v=vs.110).aspx> https://msdn.microsoft.com/en-us/library/0cbhe98f( v = vs.110).aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage%28v=vs.110%29.aspx [ ^ ]。



-S A

Dear Experts, below is my code. I'm saving an image with add some contents to the image from DrawString. My problem is, once I saved the image, CMYK become RGB, image size is different, image dpi is different. Like that several input image configurations were changed.

I just want to keep the same input configuration and quality with modifying the content only. Please help me.

Bitmap bmp = new Bitmap(Image.FromFile(@"D:\input.jpg"));

Graphics gr = Graphics.FromImage(bmp);


using (Font myFont = new Font("Arial", 42))
{
gr.DrawString("Hello!", myFont, Brushes.Green, new Point(2, 2));
gr.DrawString(date.Text, myFont, Brushes.Black, new Point(700, 798));
gr.DrawString(shape.Text, myFont, Brushes.Black, new Point(700, 908));
gr.DrawString(meas.Text, myFont, Brushes.Black, new Point(700, 1031));
gr.DrawString(weight.Text, myFont, Brushes.Black, new Point(700, 1160));
}
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);

EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 300L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp.Save(@"D:\test.jpg", jgpEncoder, myEncoderParameters);

解决方案

This is weird, but, first of all, eliminate new Bitmap(Image). Use

Bitmap bmp = new Bitmap(fileName);


And if you really wanted to initialize a bitmap from some abstract Image instance when a file is not accessible, and if you also wanted to change the size and PixelFormat, you would need to initialize an empty bitmap with required properties, and then open an instance of Image separately, and draw in on your bitmap, using the Graphics instance you would obtain exactly the way you already did (called gr in your code). In this particular case, you just don't need it, because you can open a bitmap directly from a file, which should preserver the pixel format, size and all other properties. Please see:
https://msdn.microsoft.com/en-us/library/system.drawing.bitmap%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/0cbhe98f(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage%28v=vs.110%29.aspx[^].

—SA


这篇关于C#图像内容修改和保存输入配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆