reBonjour,

J'ai écris le code suivant pour créer une image à partir d'une image et d'un texte. L'image se place en haut et le texte en dessous.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 
public static System.Drawing.Bitmap LibelledImage(System.Drawing.Bitmap originalImage, string text, System.Drawing.Font font,
    System.Drawing.Size size, int imageHeight, int textHeight)
{
 
    // Create target bitmap to return
    System.Drawing.Bitmap targetBmp = new System.Drawing.Bitmap(size.Width, size.Height);
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(targetBmp);
 
    // Fill with color
    g.FillRectangle(System.Drawing.Brushes.AntiqueWhite, 0, 0, targetBmp.Width, targetBmp.Height);
 
    // Calculate image origin point and draw image
    System.Drawing.Point originImage = new System.Drawing.Point();
    originImage.X = (size.Width - originalImage.Width) / 2;
    originImage.Y = (imageHeight - originalImage.Height) / 2;
    g.DrawImage(originalImage, originImage);
 
    // Define format to draw text
    System.Drawing.StringFormat format = new System.Drawing.StringFormat(System.Drawing.StringFormatFlags.NoClip);
    format.LineAlignment = System.Drawing.StringAlignment.Center;
    format.Alignment = System.Drawing.StringAlignment.Center;
 
    // Draw text in RectangleF
    //System.Drawing.RectangleF rectText = new System.Drawing.RectangleF(0, imageHeight, size.Width, size.Height);
    //g.DrawString(text, font, System.Drawing.Brushes.Black, rectText, format);
 
    // Draw text on origin point
    System.Drawing.Point originText = new System.Drawing.Point();
    originText.X = size.Width / 2;
    originText.Y = imageHeight + (textHeight / 2);
    g.DrawString(text, font, System.Drawing.Brushes.Black, originText, format);
 
    return targetBmp;
}
2 problèmes se posent:

1 - Quand j'écris le texte sur le point d'origine, ça fonctionne. Le texte est centré là où il faut. Mais quand j'utilise l'écriture dans le RectangleF, rien ne s'affiche. Pourtant, après vérification, la taille et position du rectangle est correcte.

2 - Si mon texte est trop long pour tenir dans le rectangle, est-ce qu'il y a un "truc" pour qu'il se mette sur 2 lignes sans que j'ai quelque chose à calculer ? Sinon, y a t-il une façon de calculer facilement comment couper mon texte ?

Merci de votre aide et bon appétit

Papy