gpt4 book ai didi

c# - 从图形转换为位图时为空位图

转载 作者:行者123 更新时间:2023-12-04 05:38:12 25 4
gpt4 key购买 nike

我使用一个库从这里绘制轮廓文本 http://www.codeproject.com/Articles/42529/Outline-Text

我写了一个测试函数,我试图将outlineText图形对象保存为位图
图形对象在控件上绘制正确,但是当我将其另存为图像时,结果图像为空

        Graphics graphic = this .CreateGraphics();
graphic.SmoothingMode = SmoothingMode.AntiAlias;
StringFormat fmt = new StringFormat(StringFormat.GenericTypographic);
Int32 lNum = (Int32)Math.Log((Double)this.TextAlign, 2);
fmt.LineAlignment = (StringAlignment)(lNum / 4);
fmt.Alignment = (StringAlignment)(lNum % 4);

OutlineText m_OutlineText = new OutlineText();

m_OutlineText.EnableShadow(true);
m_OutlineText.SetNullShadow();
m_OutlineText.Shadow(ShadowColor, ShadowSize, new Point(4, 4));
FontFamily fontFamily = this.Font.FontFamily;
float fStartX = 0.0f;
float fStartY = 0.0f;
float fDestWidth = 0.0f;
float fDestHeight = 0.0f;
m_OutlineText.MeasureString(
graphic,
fontFamily,
this.Font.Style,
(int)this.Font.Size,
this.Text,
new Point(10, 10),
fmt,
ref fStartX,
ref fStartY,
ref fDestWidth,
ref fDestHeight);

LinearGradientBrush gradientBrush = new LinearGradientBrush(new RectangleF(fStartX, fStartY, fDestWidth - (fStartX - 10), fDestHeight - (fStartY - 10)),
GrediantA, GrediantB, LinearGradientMode.Vertical);
m_OutlineText.TextOutline(gradientBrush, OutlineColor, OutlineSize);
if (_myRc == null)
{
_myRc = ClientRectangle;
}
m_OutlineText.DrawString(graphic, fontFamily, this.Font.Style, (int)this.Font.Size, this.Text, _myRc, fmt);


_textImage = new Bitmap(this.Width, this.Height, graphic);
_textImage.Save(@"C:\bmp.jpg");

最佳答案

构造函数: Bitmap(Int32, Int32, Graphics) 用指定的大小和指定的 Graphics 对象的分辨率初始化 Bitmap 类的新实例。它应该是空的。将仅使用图形的分辨率。

如果你想在位图上绘制一些东西并保存它,那么从位图创建图形:

using(Bitmap bitmap = new Bitmap(Width, Height))
using (Graphics graphics = Graphics.FromImage(bitmap))
{
Rectangle rect = new Rectangle(0, 0, Width, Height);
graphics.FillRectangle(new SolidBrush(BackColor), rect);
graphics.DrawString("Hello, World", Font, new SolidBrush(ForeColor), rect);
bitmap.Save(@"D:\hello_world.bmp");
}

如果要在控件上绘制并将其图像保存到文件,请使用 Control.DrawToBitmap方法。

BTW 位图应该有 .bmp扩展名,它不是 JPEG 图像。

关于c# - 从图形转换为位图时为空位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11643820/

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