Gdiplus抽绳到位图 [英] Gdiplus Drawstring into Bitmaps
本文介绍了Gdiplus抽绳到位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨大家好,
i尝试使用gdiplus创建字体位图,我正在使用抽象字符串和位图保存选项..它实际上应该在位图文件中保存一个字符相反它只是输出一个黑色填充的矩形......请在代码中告诉我Wats错误...
i也尝试使用lockbits和unlockbits来绘制字符串。 。
Hi folks,
i am trying to create font bitmaps using gdiplus, am using drawstring and bitmap save option.. It should actually save a character in the bitmap file rather its just outputting a black filled rectangle... pls advice me wats wrong in the code...
i have also tried using lockbits and unlockbits to draw a string..
CString szFile;
m_edtFile.GetWindowText(szFile);
Gdiplus::Status nResults = m_fontcollection.AddFontFile(szFile);
if (nResults != Gdiplus::Ok)
{
MessageBox(L"Font add fails", L"Error");
}
CPaintDC dc(this);
using namespace Gdiplus;
Gdiplus::Bitmap bitmap(L"C:\\Users\\dc230\\Desktop\\New folder\\map.bmp");
Graphics graphics(dc.m_hDC);
graphics.FromImage(&bitmap);
graphics.SetSmoothingMode(SmoothingModeAntiAlias); //sets the rendering quality of the Graphics object
graphics.SetTextRenderingHint(TextRenderingHintSingleBitPerPixelGridFit); //method to specify the type of antialiasing (if any) applied to text
SolidBrush brush(Color(255,255,255));
FontFamily fontFamily;
int nNumFound=0;
m_fontcollection.GetFamilies(1,&fontFamily,&nNumFound);
if (nNumFound > 0)
{
Gdiplus::Font font(&fontFamily,28,FontStyleRegular,UnitPixel);
StringFormat strformat;
strformat.SetAlignment(StringAlignmentCenter);
wchar_t buf[] = {L"A"};
RectF therect;
therect.Height = 10;
therect.Width = 10;
therect.X = 5;
therect.Y = 5;
Gdiplus::Status st =graphics.DrawString(buf,wcslen(buf),&font,therect,&strformat,&brush);
graphics.Save();
Gdiplus::Bitmap b(100,100,&graphics);
graphics.FromImage(&b);
Gdiplus::BitmapData bmpd= {};
Rect therect1;
therect.Height = 50;
therect.Width = 30;
therect.X = 20;
therect.Y = 10;
bitmap.LockBits(&therect1, ImageLockModeWrite,PixelFormat32bppARGB,&bmpd);
::glTexImage2D( GL_TEXTURE_2D,0,GL_RGBA ,50,50,0,GL_RGBA,GL_BITMAP,0);
bitmap.UnlockBits(&bmpd);
// Rotate bitmap 180 degrees
b.RotateFlip(Rotate180FlipNone);
if (b.GetLastStatus() == Ok)
{
EncoderParameters pEncoderParameters;
pEncoderParameters.Count = 1;
pEncoderParameters.Parameter[0].Guid = EncoderCompression;
pEncoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
pEncoderParameters.Parameter[0].NumberOfValues = 1;
// Save the image as a bmp
ULONG compression = EncoderValueCompressionRle;
pEncoderParameters.Parameter[0].Value = &compression;
CLSID encoderClsid;
GetEncoderClsid(L"image/bmp", &encoderClsid);
b.Save(L"C:\\Users\\dc230\\Desktop\\New folder\\map1.bmp",&encoderClsid, &pEncoderParameters);
MessageBox(L"Image Got Saved !");
}
else
{
MessageBox(L"Unable to save the bitmap");
}
}
推荐答案
i刚刚在C#中找到了解决方案
private void btnRender_Click(object sender,EventArgs e)
{
// string file =C:\\Test \\ELEPHNT.ttf;
string file =C:\\Test \\\ \\ aparaj.ttf;
System.Drawing.Text.PrivateFontCollection pf = new System.Drawing.Text.PrivateFontCollection();
pf.AddFontFile(file);
FontFamily [] fontFamilies;
fontFamilies = pf.Families;
字体f =新字体(fontFamilies [0],70);
for(int i = 550; i& lt; 1600; i ++)
{
位图位图=新位图(400,400);
图形g = Graphics.FromImage(位图);
//设置文本呈现特征
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.TextRenderingHint = System。 Drawing.Text.TextRenderingHint.ClearTypeGridFit;
g.Clear(Color.White);
试试
{
string stChar = char.ConvertFromUtf32(i);
TextRenderer.DrawText(g,stChar,f,new Point(100,200),Color.Black);
bitmap.Save(C:\\Test \\JPG \\+ stChar +.jpg,System.Drawing.Imaging.ImageFormat.Jpeg); < br $>
}
catch(exception ex)
{
MessageBox.Show(ex.Message);
}
}
Rgds
Yosotha
Hi,
i have just found a solution in C#
private void btnRender_Click(object sender, EventArgs e)
{
//string file = "C:\\Test\\ELEPHNT.ttf";
string file = "C:\\Test\\aparaj.ttf";
System.Drawing.Text.PrivateFontCollection pf = new System.Drawing.Text.PrivateFontCollection();
pf.AddFontFile(file);
FontFamily[] fontFamilies;
fontFamilies = pf.Families;
Font f = new Font(fontFamilies[0], 70);
for (int i = 550; i < 1600; i++)
{
Bitmap bitmap = new Bitmap(400, 400);
Graphics g = Graphics.FromImage(bitmap);
// set the text rendering characteristics
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
g.Clear(Color.White);
try
{
string stChar = char.ConvertFromUtf32(i);
TextRenderer.DrawText(g, stChar, f, new Point(100, 200), Color.Black);
bitmap.Save("C:\\Test\\JPG\\" + stChar + ".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Rgds
Yosotha
这篇关于Gdiplus抽绳到位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文