C#图像内容修改和保存输入配置 [英] C# Image Content Modification and Save with input configurations
问题描述
我只想保持相同的输入配置和质量,只修改内容。请帮帮我。
位图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, eliminatenew Bitmap(Image)
. Use
Bitmap bmp = new Bitmap(fileName);
And if you really wanted to initialize a bitmap from some abstractImage
instance when a file is not accessible, and if you also wanted to change the size andPixelFormat
, you would need to initialize an empty bitmap with required properties, and then open an instance ofImage
separately, and draw in on your bitmap, using theGraphics
instance you would obtain exactly the way you already did (calledgr
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屋!