gpt4 book ai didi

c# - 在图像库上绘制文本

转载 作者:行者123 更新时间:2023-11-30 14:37:36 25 4
gpt4 key购买 nike

在C#中是否有用于将文本绘制为图像的开源库?我整天都在与 TextRenderergraphics.DrawString() 作斗争,但我从未接近获得不错的结果,我尝试了平滑、插值、TextRenderHint 的所有组合,但质量总是半体面的。

这里有一些图片,这是我得到的最好的:

enter image description here

它需要看起来像什么:

enter image description here

这看起来确实不错,但是对于某些字符串,某些字母的字符间距似乎有误,并且字符串倾斜。

设置是:

objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
objGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.GammaCorrected;
objGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
objGraphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
objGraphics.TextContrast = 0;

格式为PNG,背景透明,方法为TextRenderer.Drawtext()。似乎文本的粗细不对,我认为平滑有问题,当我尝试加粗文本时它几乎保持不变,但字体大小仅为 ~10px。

最佳答案

这是我用来为上传到我网站的照片添加版权水印的方法:

    //Add Watermark to photo.
private System.Drawing.Image CreateWatermark(System.Drawing.Image imgPhoto, string Copyright)
{
Graphics g = Graphics.FromImage(imgPhoto);

g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;

foreach (PropertyItem pItem in imgPhoto.PropertyItems)
{
imgPhoto.SetPropertyItem(pItem);
}

int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;

//create a Bitmap the Size of the original photograph
Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

//load the Bitmap into a Graphics object
Graphics grPhoto = Graphics.FromImage(bmPhoto);

//------------------------------------------------------------
//Step #1 - Insert Copyright message
//------------------------------------------------------------

//Set the rendering quality for this Graphics object
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

//Draws the photo Image object at original size to the graphics object.
grPhoto.DrawImage(
imgPhoto, // Photo Image object
new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure
0, // x-coordinate of the portion of the source image to draw.
0, // y-coordinate of the portion of the source image to draw.
phWidth, // Width of the portion of the source image to draw.
phHeight, // Height of the portion of the source image to draw.
GraphicsUnit.Pixel); // Units of measure

//-------------------------------------------------------
//to maximize the size of the Copyright message we will
//test multiple Font sizes to determine the largest posible
//font we can use for the width of the Photograph
//define an array of point sizes you would like to consider as possiblities
//-------------------------------------------------------
int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };

Font crFont = null;
SizeF crSize = new SizeF();

//Loop through the defined sizes checking the length of the Copyright string
//If its length in pixles is less then the image width choose this Font size.
for (int i = 0; i < 7; i++)
{
//set a Font object to Arial (i)pt, Bold
crFont = new Font("arial", sizes[i], FontStyle.Bold);
//Measure the Copyright string in this Font
crSize = grPhoto.MeasureString(Copyright, crFont);

if ((ushort)crSize.Width < (ushort)phWidth)
break;
}

//Since all photographs will have varying heights, determine a
//position 5% from the bottom of the image
int yPixlesFromBottom = (int)(phHeight * .05);

//Now that we have a point size use the Copyrights string height
//to determine a y-coordinate to draw the string of the photograph
float yPosFromBottom = ((phHeight - yPixlesFromBottom) - (crSize.Height / 2));

//Determine its x-coordinate by calculating the center of the width of the image
float xCenterOfImg = (phWidth / 2);

//Define the text layout by setting the text alignment to centered
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Near;

//define a Brush which is semi trasparent black (Alpha set to 153)
SolidBrush semiTransBrush2 = new SolidBrush(System.Drawing.Color.FromArgb(153, 0, 0, 0));

//Draw the Copyright string
grPhoto.DrawString(Copyright, //string of text
crFont, //font
semiTransBrush2, //Brush
new PointF(xCenterOfImg + 1, yPosFromBottom + 1), //Position
StrFormat);

//define a Brush which is semi trasparent white (Alpha set to 153)
SolidBrush semiTransBrush = new SolidBrush(System.Drawing.Color.FromArgb(153, 255, 255, 255));

//Draw the Copyright string a second time to create a shadow effect
//Make sure to move this text 1 pixel to the right and down 1 pixel
grPhoto.DrawString(Copyright, //string of text
crFont, //font
semiTransBrush, //Brush
new PointF(xCenterOfImg, yPosFromBottom), //Position
StrFormat); //Text alignment
imgPhoto = bmPhoto;
return imgPhoto;
}

关于c# - 在图像库上绘制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9049741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com