c#在位图上写文本
我有以下问题。 我想在c#窗体中做一些graphics。 我想读取位图到我的程序,然后在这个位图上写一些文字。 最后我想把这个图片加载到pictureBox。 这是我的问题。 我该怎么做?
例如,它如何工作:
Bitmap a = new Bitmap(@"path\picture.bmp"); a.makeTransparent(); // ? a.writeText("some text", positionX, positionY); pictuteBox1.Image = a;
有可能吗?
Bitmap bmp = new Bitmap("filename.bmp"); RectangleF rectf = new RectangleF(70, 90, 90, 50); Graphics g = Graphics.FromImage(bmp); g.SmoothingMode = SmoothingMode.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.DrawString("yourText", new Font("Tahoma",8), Brushes.Black, rectf); g.Flush(); image.Image=bmp;
非常古老的问题,但只是今天build立一个应用程序,发现其他答案中显示的设置不会导致一个干净的形象(可能因为新的选项添加到以后的.Net版本)。
假设你想要位图中心的文本,你可以这样做:
// Load the original image Bitmap bmp = new Bitmap("filename.bmp"); // Create a rectangle for the entire bitmap RectangleF rectf = new RectangleF(0, 0, bmp.Width, bmp.Height); // Create graphic object that will draw onto the bitmap Graphics g = Graphics.FromImage(bmp); // ------------------------------------------ // Ensure the best possible quality rendering // ------------------------------------------ // The smoothing mode specifies whether lines, curves, and the edges of filled areas use smoothing (also called antialiasing). One exception is that path gradient brushes do not obey the smoothing mode. Areas filled using a PathGradientBrush are rendered the same way (aliased) regardless of the SmoothingMode property. g.SmoothingMode = SmoothingMode.AntiAlias; // The interpolation mode determines how intermediate values between two endpoints are calculated. g.InterpolationMode = InterpolationMode.HighQualityBicubic; // Use this property to specify either higher quality, slower rendering, or lower quality, faster rendering of the contents of this Graphics object. g.PixelOffsetMode = PixelOffsetMode.HighQuality; // This one is important g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; // Create string formatting options (used for alignment) StringFormat format = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; // Draw the text onto the image g.DrawString("yourText", new Font("Tahoma",8), Brushes.Black, rectf, format); // Flush all graphics changes to the bitmap g.Flush(); // Now save or use the bitmap image.Image = bmp;
参考
- https://msdn.microsoft.com/en-us/library/system.drawing.graphics.smoothingmode(v=vs.110).aspx
- https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.interpolationmode(v=vs.110).aspx
- https://msdn.microsoft.com/en-us/library/system.drawing.graphics.pixeloffsetmode(v=vs.110).aspx
- https://msdn.microsoft.com/en-us/library/system.drawing.graphics.textrenderinghint(v=vs.110).aspx
- https://msdn.microsoft.com/en-us/library/system.drawing.stringformat(v=vs.110).aspx
- https://msdn.microsoft.com/en-us/library/21kdfbzs(v=vs.110).aspx
您需要使用Graphics
类才能在位图上写入。
具体来说,其中一个DrawString
方法。
Bitmap a = new Bitmap(@"path\picture.bmp"); using(Graphics g = Graphics.FromImage(a)) { g.DrawString(....); // requires font, brush etc } pictuteBox1.Image = a;
var bmp = new Bitmap(@"path\picture.bmp"); using( Graphics g = Graphics.FromImage( bmp ) ) { g.DrawString( ... ); } picturebox1.Image = bmp;
如果你想包装你的文字,那么你应该画一个矩形的文字:
RectangleF rectF1 = new RectangleF(30, 10, 100, 122); e.Graphics.DrawString(text1, font1, Brushes.Blue, rectF1);
请参阅: https : //msdn.microsoft.com/en-us/library/baw6k39s(v=vs.110).aspx