Convertir texte en BMP en mémoire

DIMUSERS Messages postés 37 Date d'inscription jeudi 8 juin 2006 Statut Membre Dernière intervention 2 septembre 2010 - 6 janv. 2008 à 15:37
DIMUSERS Messages postés 37 Date d'inscription jeudi 8 juin 2006 Statut Membre Dernière intervention 2 septembre 2010 - 10 janv. 2008 à 19:20
Bonjour à tous,
Voici mon problème :
Je souhaiterai convertir une chaîne de caractères formatée (exemple avec un soulignement et en italique) en un BitMap, dans le but de transformer ce BitMap en Texture. Idéalement, je souhaiterai que cette chaine n'apparaisse pas à l'écran.
Partant d'un BitMap en mémoire, je pense savoir le transformer en texture (via un stream). Mais je n'arrive pas a faire la première partie.
Je tourne gravement en rond.
Avez vous des idées ?

7 réponses

DIMUSERS Messages postés 37 Date d'inscription jeudi 8 juin 2006 Statut Membre Dernière intervention 2 septembre 2010
6 janv. 2008 à 16:31
J'ai oublié une précision importante, le texte doit être formaté avec le GDI (TextOut ou DrawText).

st.antoine
0
cs_niky Messages postés 168 Date d'inscription jeudi 28 juin 2001 Statut Membre Dernière intervention 18 octobre 2008 7
7 janv. 2008 à 14:25
Salut,

Je pense que la première étape consiste à créer ta bitmap (via le constructeur de System.Drawing.Bitmap).
Ensuite, tu créés un objet System.Drawing.Graphics (via la méthode statique FromImage de la classe Graphics). Cette objet Graphics va te permettre de dessiner sur la bitmap (méthodes DrawString).
0
DIMUSERS Messages postés 37 Date d'inscription jeudi 8 juin 2006 Statut Membre Dernière intervention 2 septembre 2010
7 janv. 2008 à 21:54
Merci niky, en effet c'est la bonne méthode, mais avec le GDI+


Je poste le programme à la suite. Mais la difficulté que je n'arrive pas à surmonter est de faire la même chose avec le GDI c'est à dire en utilisant soit  gdi.dll et TexOut, soit le TextRenderer.
Merci de votre aide.// LE CODE EN GDI+



using



System;


using



System.Collections.Generic;


using



System.ComponentModel;


using



System.Data;


using



System.Drawing;


using



System.Text;


using



System.Windows.Forms;


using



System.Runtime.InteropServices;


using






WinFont
= System.Drawing.

Font
;


namespace



App01{


public



partial



class



Form1
:

Form

{







String
Chaine;


private



void
Test(){


Bitmap
bmp =

new



Bitmap
(200,

this
.Font.Height );


Graphics
gBmp =

Graphics
.FromImage(bmp);


SolidBrush
brLine =

new



SolidBrush
(

Color
.Black);gBmp.DrawString(Chaine,


this
.Font, brLine,

new



Point
(0, 0));bmp.Save(


"c:\\xxx.bmp"
);}


public
Form1(){


this
.Show();Chaine =


"Hello"
;Test();

}

}

}
// FIN





st.antoine
0
DIMUSERS Messages postés 37 Date d'inscription jeudi 8 juin 2006 Statut Membre Dernière intervention 2 septembre 2010
7 janv. 2008 à 21:54
Merci niky, en effet c'est la bonne méthode, mais avec le GDI+


Je poste le programme à la suite. Mais la difficulté que je n'arrive pas à surmonter est de faire la même chose avec le GDI c'est à dire en utilisant soit  gdi.dll et TexOut, soit le TextRenderer.
Merci de votre aide.// LE CODE EN GDI+



using



System;


using



System.Collections.Generic;


using



System.ComponentModel;


using



System.Data;


using



System.Drawing;


using



System.Text;


using



System.Windows.Forms;


using



System.Runtime.InteropServices;


using






WinFont
= System.Drawing.

Font
;


namespace



App01{


public



partial



class



Form1
:

Form

{







String
Chaine;


private



void
Test(){


Bitmap
bmp =

new



Bitmap
(200,

this
.Font.Height );


Graphics
gBmp =

Graphics
.FromImage(bmp);


SolidBrush
brLine =

new



SolidBrush
(

Color
.Black);gBmp.DrawString(Chaine,


this
.Font, brLine,

new



Point
(0, 0));bmp.Save(


"c:\\xxx.bmp"
);}


public
Form1(){


this
.Show();Chaine =


"Hello"
;Test();

}

}

}
// FIN





st.antoine
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_niky Messages postés 168 Date d'inscription jeudi 28 juin 2001 Statut Membre Dernière intervention 18 octobre 2008 7
7 janv. 2008 à 22:42
Pourrais-tu éclaircir ce que tu cherches à faire avec cette images (création de la texture) ?

En fait, System.Drawing n'est qu'une version managée de GDI+ (http://msdn2.microsoft.com/fr-fr/library/d420az6e(VS.80).aspx).

Un System.Drawing.Bitmap est en mémoire une image manipulable directement avec les API GDI+. Tu peux d'ailleurs obtenir un pointeur dessus (avec LockBits), une image compatible GDI (avec GetHBitmap) ou le contexte de périphérique (hDC).
0
DIMUSERS Messages postés 37 Date d'inscription jeudi 8 juin 2006 Statut Membre Dernière intervention 2 septembre 2010
8 janv. 2008 à 19:00
Merci de ta réponse, en fait j'utilise pour afficher du texte le GDI qui est annoncé par Microsoft comme plus rapide que le GDI+. J'ai besoin d'afficher du texte formatté composé de différentes fontes et couleurs pendant un intervalle de temps très court (entre 2 images d'un mpeg).


Je voudrai obtenir le même résultat que les programme ci-dessus mais en utilisant TextOut, c'est à dire créer une image à partir d'un affichage de texte. J'utiliserai ensuite cette image comme texture d'un sprite que je ferai défiler sous mon image (Une sorte de sous-titre mais qui défilerait)


Voilà ce que j'ai écrit qui ne fonctionne pas (en fait j'obtiens une image noire).


using System;



using



System.Collections.Generic;


using



System.ComponentModel;


using



System.Data;


using



System.Drawing;


using



System.Text;


using



System.Windows.Forms;


using



Microsoft.DirectX;


using



Microsoft.DirectX.Direct3D;


using



System.Runtime.InteropServices;


using






Device
= Microsoft.DirectX.Direct3D.

Device
;


using






D3DFont
= Microsoft.DirectX.Direct3D.

Font
;


using






WinFont
= System.Drawing.

Font
;


namespace



App01{


public



partial



class



Form1
:

Form

{







public



enum



TernaryRasterOperations
:

uint

{


SRCCOPY = 0x00CC0020,




/* dest = source*/

SRCPAINT = 0x00EE0086,




/* dest = source OR dest*/

SRCAND = 0x008800C6,




/* dest = source AND dest*/

SRCINVERT = 0x00660046,




/* dest = source XOR dest*/

SRCERASE = 0x00440328,




/* dest = source AND (NOT dest )*/

NOTSRCCOPY = 0x00330008,




/* dest = (NOT source)*/

NOTSRCERASE = 0x001100A6,




/* dest = (NOT src) AND (NOT dest) */

MERGECOPY = 0x00C000CA,




/* dest = (source AND pattern)*/

MERGEPAINT = 0x00BB0226,




/* dest = (NOT source) OR dest*/

PATCOPY = 0x00F00021,




/* dest = pattern*/

PATPAINT = 0x00FB0A09,




/* dest = DPSnoo*/

PATINVERT = 0x005A0049,




/* dest = pattern XOR dest*/

DSTINVERT = 0x00550009,




/* dest = (NOT dest)*/

BLACKNESS = 0x00000042,




/* dest = BLACK*/

WHITENESS = 0x00FF0062,




/* dest = WHITE*/

};


[




DllImport
(

"gdi32.dll"
)]


public



static



extern



bool
BitBlt(

IntPtr
hObject,

int
nXDest,

int
nYDest,

int
nWidth,


int
nHeight,

IntPtr
hObjSource,

int
nXSrc,

int
nYSrc,

TernaryRasterOperations
dwRop);[


DllImport
(

"Gdi32.dll"
)]


public



static



extern



bool
TextOut(


IntPtr
hWnd,

// handle to DC






int
nXStart,

// x-coordinate of starting position






int
nYStart,

// y-coordinate of starting position






string
lpString,

// character string






int
cbString

// number of characters

);


[




StructLayout
(

LayoutKind
.Sequential)]


// Conversion bmp






public



struct



ABC

{




/* abc */






public



int
abcA;


public



uint
abcB;


public



int
abcC;}

[


DllImport
(

"gdi32.dll"
)]


static



extern



bool
GetCharABCWidths(

IntPtr
hdc,

uint
uFirstChar,


uint
uLastChar,

ref



ABC
lpabc);


//Paramètres d'affichage






PresentParameters
_presentParams;


//Device d'affichage






private



Device
_device;


private



bool
_affichageImmediat;[


DllImport
(

"gdi32.dll"
)]


static



extern



IntPtr
CreateCompatibleBitmap(

IntPtr
hdc,

int
nWidth,


int
nHeight);[


DllImport
(

"gdi32.dll"
, SetLastError =

true
)]


static



extern



IntPtr
CreateCompatibleDC(

IntPtr
hdc);[


DllImport
(

"gdi32.dll"
)]


static



extern



IntPtr
SelectObject(

IntPtr
hdc,

IntPtr
hgdiobj);[


DllImport
(

"user32.dll"
)]


static



extern



int
ReleaseDC(

IntPtr
hWnd,

IntPtr
hDC);[


DllImport
(

"gdi32.dll"
, ExactSpelling =

true
, SetLastError =

true
)]


static



extern



bool
DeleteObject(

IntPtr
hObject);[


DllImport
(

"gdi32.dll"
, ExactSpelling =

true
, SetLastError =

true
)]


static



extern



bool
DeleteDC(

IntPtr
hdc);[


DllImport
(

"gdi32.dll"
)]


static



extern



uint
SetTextColor(

IntPtr
hdc,

int
crColor);[


DllImport
(

"gdi32.dll"
)]


static



extern



uint
SetBkColor(

IntPtr
hdc,

int
crColor);[


DllImport
(

"gdi32.dll"
)]


public



static



extern



int
SetBkMode(

IntPtr
hdc,

int
BkMode);


private



Texture
_texture;


private
System.IO.

MemoryStream
_stream;


// Fontes

[




DllImport
(

"Gdi32.dll"
)]


static



extern



IntPtr
CreateFontIndirect(

LOGFONT
logFont);

// characteristics

[




StructLayout
(

LayoutKind
.Sequential, CharSet =

CharSet
.Auto)]


public



class



LOGFONT

{







public



int
lfHeight = 0;


public



int
lfWidth = 0;


public



int
lfEscapement = 0;


public



int
lfOrientation = 0;


public



int
lfWeight = 400;


public



byte
lfItalic = 0;


public



byte
lfUnderline = 0;


public



byte
lfStrikeOut = 0;


public



byte
lfCharSet = 0;


public



byte
lfOutPrecision = 0;


public



byte
lfClipPrecision = 0;


public



byte
lfQuality = 0;


public



byte
lfPitchAndFamily = 0;[


MarshalAs
(

UnmanagedType
.ByValTStr, SizeConst = 32)]


public



string
lfFaceName =

string
.Empty;}

 

 


public



const



int
OPAQUE = 2;


LOGFONT
logFont =

new



LOGFONT
();


IntPtr
ActiveCurrentFontHandle;


Graphics
GTxtOut;


IntPtr
HdcTxtOut;


String
Chaine;System.Drawing.


Font
localFont =

new
System.Drawing.

Font
(

"Agency FB"
, 25F, System.Drawing.

FontStyle
.Regular, System.Drawing.

GraphicsUnit
.Point, ((

byte
)(0)));


private



void
Test(){


Bitmap
bmp =

new



Bitmap
(200,localFont.Height );


Graphics
gBmp =

Graphics
.FromImage(bmp);


SolidBrush
brLine =

new



SolidBrush
(

Color
.Black);


// Create a GDI bitmap from GDI+

HdcTxtOut = bmp.GetHbitmap();







// Fill the GDI font structure






this
.localFont.ToLogFont(logFont);


// Set characteristics of TextOut output

SetBkMode(HdcTxtOut, OPAQUE);


SetTextColor(HdcTxtOut,




ColorTranslator
.ToWin32(

Color
.Black));SetBkColor(HdcTxtOut,


ColorTranslator
.ToWin32(

Color
.Red));


// Create the GDI Font

ActiveCurrentFontHandle = CreateFontIndirect(logFont);







// Get a compatible bitmap with the created device context






IntPtr
DcHdcBmpTxtOut = CreateCompatibleBitmap(HdcTxtOut, 200, localFont.Height);


// Assign the font to the curent device context

SelectObject(HdcTxtOut, ActiveCurrentFontHandle);







// Assign the compatible bitmap to the device context



// ????????????????????????????????????????????????
// C'est surement ce qui suit qui est faux, mais je sèche
// ????????????????????????????????????????????????

SelectObject(HdcTxtOut, DcHdcBmpTxtOut);


TextOut(HdcTxtOut, 0, 0,




this
.Chaine,

this
.Chaine.Length);bmp.Save(


"c:\\yyy.bmp"
);}


public
Form1(){


this
.Show();Chaine =


"Bonjour"
;Test();

}

}

}


Merci pour vos suggestions
0
DIMUSERS Messages postés 37 Date d'inscription jeudi 8 juin 2006 Statut Membre Dernière intervention 2 septembre 2010
10 janv. 2008 à 19:20
Voilà,
Je ne suis pas peu fier de vous présenter le code qui marche (merci à http://www.vb-helper.com/howto_memory_bitmap_text.html)


using System;



using



System.Collections.Generic;


using



System.ComponentModel;


using



System.Data;


using



System.Drawing;


using



System.Text;


using



System.Windows.Forms;


using



Microsoft.DirectX;


using



Microsoft.DirectX.Direct3D;


using



System.Runtime.InteropServices;


using






Device
= Microsoft.DirectX.Direct3D.

Device
;


using






D3DFont
= Microsoft.DirectX.Direct3D.

Font
;


using






WinFont
= System.Drawing.

Font
;


namespace



App01{


public



partial



class



Form1
:

Form

{







public



enum



TernaryRasterOperations
:

uint

{


SRCCOPY = 0x00CC0020,




/* dest = source*/

SRCPAINT = 0x00EE0086,




/* dest = source OR dest*/

SRCAND = 0x008800C6,




/* dest = source AND dest*/

SRCINVERT = 0x00660046,




/* dest = source XOR dest*/

SRCERASE = 0x00440328,




/* dest = source AND (NOT dest )*/

NOTSRCCOPY = 0x00330008,




/* dest = (NOT source)*/

NOTSRCERASE = 0x001100A6,




/* dest = (NOT src) AND (NOT dest) */

MERGECOPY = 0x00C000CA,




/* dest = (source AND pattern)*/

MERGEPAINT = 0x00BB0226,




/* dest = (NOT source) OR dest*/

PATCOPY = 0x00F00021,




/* dest = pattern*/

PATPAINT = 0x00FB0A09,




/* dest = DPSnoo*/

PATINVERT = 0x005A0049,




/* dest = pattern XOR dest*/

DSTINVERT = 0x00550009,




/* dest = (NOT dest)*/

BLACKNESS = 0x00000042,




/* dest = BLACK*/

WHITENESS = 0x00FF0062,




/* dest = WHITE*/

};


[




DllImport
(

"gdi32.dll"
)]


public



static



extern



bool
BitBlt(

IntPtr
hObject,

int
nXDest,

int
nYDest,

int
nWidth,


int
nHeight,

IntPtr
hObjSource,

int
nXSrc,

int
nYSrc,

TernaryRasterOperations
dwRop);[


DllImport
(

"Gdi32.dll"
)]


public



static



extern



bool
TextOut(


IntPtr
hWnd,

// handle to DC






int
nXStart,

// x-coordinate of starting position






int
nYStart,

// y-coordinate of starting position






string
lpString,

// character string






int
cbString

// number of characters

);


[




StructLayout
(

LayoutKind
.Sequential)]


// Conversion bmp






public



struct



ABC

{




/* abc */






public



int
abcA;


public



uint
abcB;


public



int
abcC;}

[


DllImport
(

"gdi32.dll"
)]


static



extern



bool
GetCharABCWidths(

IntPtr
hdc,

uint
uFirstChar,


uint
uLastChar,

ref



ABC
lpabc);


//Paramètres d'affichage






PresentParameters
_presentParams;


//Device d'affichage






private



Device
_device;


private



bool
_affichageImmediat;[


DllImport
(

"gdi32.dll"
)]


static



extern



IntPtr
CreateCompatibleBitmap(

IntPtr
hdc,

int
nWidth,


int
nHeight);[


DllImport
(

"gdi32.dll"
, SetLastError =

true
)]


static



extern



IntPtr
CreateCompatibleDC(

IntPtr
hdc);[


DllImport
(

"gdi32.dll"
)]


static



extern



IntPtr
SelectObject(

IntPtr
hdc,

IntPtr
hgdiobj);[


DllImport
(

"user32.dll"
)]


static



extern



int
ReleaseDC(

IntPtr
hWnd,

IntPtr
hDC);[


DllImport
(

"gdi32.dll"
, ExactSpelling =

true
, SetLastError =

true
)]


static



extern



bool
DeleteObject(

IntPtr
hObject);[


DllImport
(

"gdi32.dll"
, ExactSpelling =

true
, SetLastError =

true
)]


static



extern



bool
DeleteDC(

IntPtr
hdc);[


DllImport
(

"gdi32.dll"
)]


static



extern



uint
SetTextColor(

IntPtr
hdc,

int
crColor);[


DllImport
(

"gdi32.dll"
)]


static



extern



uint
SetBkColor(

IntPtr
hdc,

int
crColor);[


DllImport
(

"gdi32.dll"
)]


public



static



extern



int
SetBkMode(

IntPtr
hdc,

int
BkMode);


private



Texture
_texture;


private
System.IO.

MemoryStream
_stream;


// Fontes

[




DllImport
(

"Gdi32.dll"
)]


static



extern



IntPtr
CreateFontIndirect(

LOGFONT
logFont);

// characteristics

[




StructLayout
(

LayoutKind
.Sequential, CharSet =

CharSet
.Auto)]


public



class



LOGFONT

{







public



int
lfHeight = 0;


public



int
lfWidth = 0;


public



int
lfEscapement = 0;


public



int
lfOrientation = 0;


public



int
lfWeight = 400;


public



byte
lfItalic = 0;


public



byte
lfUnderline = 0;


public



byte
lfStrikeOut = 0;


public



byte
lfCharSet = 0;


public



byte
lfOutPrecision = 0;


public



byte
lfClipPrecision = 0;


public



byte
lfQuality = 0;


public



byte
lfPitchAndFamily = 0;[


MarshalAs
(

UnmanagedType
.ByValTStr, SizeConst = 32)]


public



string
lfFaceName =

string
.Empty;}

 

 


public



const



int
OPAQUE = 2;


public



const



int
TRANSPARENT = 1;


LOGFONT
logFont =

new



LOGFONT
();


IntPtr
ActiveCurrentFontHandle;


Graphics
GTxtOut;


IntPtr
HdcTxtOut;


String
Chaine;System.Drawing.


Font
localFont =

new
System.Drawing.

Font
(

"Agency FB"
, 25F, System.Drawing.

FontStyle
.Regular, System.Drawing.

GraphicsUnit
.Point, ((

byte
)(0)));


public
Form1(){


this
.Show();Chaine =


"Victoire !"
;InitializeComponent();

GTxtOut =


this
.CreateGraphics();HdcTxtOut = GTxtOut.GetHdc();


IntPtr
DcHdcTxtOut = CreateCompatibleDC(HdcTxtOut);SetBkMode(DcHdcTxtOut, TRANSPARENT);

SetTextColor(DcHdcTxtOut,


ColorTranslator
.ToWin32(

Color
.White));SetBkColor(DcHdcTxtOut,


ColorTranslator
.ToWin32(

Color
.Black));


this
.localFont.ToLogFont(logFont);ActiveCurrentFontHandle = CreateFontIndirect(logFont);

SelectObject(DcHdcTxtOut, ActiveCurrentFontHandle);


//int Width = GetLenghtInPixel(HdcTxtOut,Chaine);






int
Height =

this
.localFont.Height;


IntPtr
DcHdcBmpTxtOut = CreateCompatibleBitmap(HdcTxtOut, Width, Height);SelectObject(DcHdcTxtOut, DcHdcBmpTxtOut);

TextOut(DcHdcTxtOut, 0, 0,


this
.Chaine,

this
.Chaine.Length);


Bitmap
Bmp = System.Drawing.

Image
.FromHbitmap(DcHdcBmpTxtOut);Bmp.Save(


"c:\\toto.bmp"
);DeleteDC(DcHdcTxtOut);

DeleteDC(DcHdcBmpTxtOut);

GTxtOut.ReleaseHdc(HdcTxtOut);

GTxtOut.Dispose();

}

}

}
0
Rejoignez-nous