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 36 37
| /// <summary>
/// Méthode pour imprimer un composant dans un fichier .png
/// </summary>
/// <param name="fe">FrameworkElement à sauvegarder</param>
/// <param name="ApplyCurrentSize">Indique si on définit une taille spécifique
/// ou si l'on prend la taille d'origine</param>
/// <param name="width">Nouvelle largueur</param>
/// <param name="height">Nouvelle hauteur</param>
/// <remarks>Extensions tolérées : .bmp .jpeg .png</remarks>
public static RenderTargetBitmap GetBitmapFromElement(FrameworkElement fe, bool ApplyCurrentSize = true,
int width = 0, int height = 0)
{
if (fe == null)
throw new NullReferenceException("FrameworkElement is null!");
//on définit la taille
RenderTargetBitmap targetBitmap = ApplyCurrentSize
? new RenderTargetBitmap((int)fe.ActualWidth, (int)fe.ActualHeight, //taille courante
96d, 96d,
PixelFormats.Default)
: new RenderTargetBitmap(width, height, //taille spécifique
96d, 96d,
PixelFormats.Default);
//on crée un fond sinon c'est le noir par défaut
DrawingVisual dv = new DrawingVisual();
DrawingContext dc = dv.RenderOpen();
Rect r = new Rect(0, 0, targetBitmap.Width, targetBitmap.Height);
dc.DrawRectangle(Brushes.White, (Pen)null, r);
dc.Close();
//on ajoute le fond et le contrôle
targetBitmap.Render(dv);
targetBitmap.Render(fe);
return targetBitmap;
} |
Partager