2014-10-29 69 views
0

我想生成字形的位圖圖像,以便可以將其與其他圖像中未知字母的像素值進行比較。使用SFML在C++中將字形渲染爲位圖圖像

我正在使用Visual Studio 2008,我的項目是在C++中,而我正在使用SFML。我可以從TTF文件罰款的字體加載,我試圖做這樣的事情:

sf::Font myFont; 
myFont.loadFromFile("Path\\arial.ttf"); 

sf::Texture myTexture = myFont.getTexture(48); 
sf::Image textureImage = myTexture.copyToImage(); 

sf::Glyph myGlyph = myFont.getGlyph(65, 12, false); // get the 'A' glyph 

sf::Image glyphImage; 
glyphImage.create(myGlyph.bounds.width, myGlyph.bounds.height, sf::Color::White); 
glyphImage.copy(textureImage, 0, 0, myGlyph.textureRect); 

我相信這也不行,因爲我剛剛從質地,其中的字形部分創建圖像位於,而不是字形本身的像素值。

有人可以幫忙嗎?

回答

0

也能正常工作對我來說:

unsigned int size = 20; 
sf::Glyph glyph = font.getGlyph('A', size, false); 
sf::Texture bitmap = font.getTexture(size); 

sf::Image image; 
image.create(glyph.bounds.width, glyph.bounds.height); 
image.copy(bitmap.copyToImage(), 0, 0, glyph.textureRect); 

請注意,此示例中的字符大小是一樣的,當getGlyphgetTexture被稱爲,這是不是在上面的代碼的情況。

另外,爲了便於閱讀,請使用'A'而不是像65這樣的幻數。

+0

太棒了,對我有用:)這是導致失敗的字符大小的差異。謝謝你的幫助! – user12066 2014-10-29 12:11:04