/ / Conversion de la page Imprimer les graphiques en bitmap C # - c #, graphiques, bitmap

Conversion de graphiques de page d'impression en bitmap C # - c #, graphiques, bitmap

J'ai une application où l'utilisateur peut imprimer un document d'éléments sélectionnés sous la forme d'une facture. Tout fonctionne bien cependant sur le PrintPage Si je souhaite capturer le document ou les graphiques, convertissez-le en bitmap afin que je puisse y sauvegarder un .bmp pour une utilisation / visualisation ultérieure. (Remarque: il y a plusieurs pages dans ce document) Je l'ai configuré comme ceci:

PrintDocument doc = new PrintDocument();
doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
doc.Print();

Puis sur le PrintPage un événement:

private void doc_PrintPage(object sender, PrintPageEventArgs ev)
{
// Use ev.Graphics to create the document
// I create the document here

// After I have drawn all the graphics I want to get it and turn it into a bitmap and save it.
}

J'ai coupé tous les ev.Graphics code juste parce qu'il y a beaucoup de lignes. Existe-t-il un moyen de transformer les graphiques en bitmap sans modifier aucun code générant des graphiques PrintDocument? Ou faire quelque chose de similaire, peut-être copier le document et le convertir en bitmap?

Réponses:

5 pour la réponse № 1

Vous devriez en fait dessiner la page dans le bitmap, puis utiliser ev.Graphics pour dessiner ce bitmap sur la page.

private void doc_PrintPage(object sender, PrintPageEventArgs ev)
{
var bitmap = new Bitmap((int)graphics.ClipBounds.Width,
(int)graphics.ClipBounds.Height);

using (var g = Graphics.FromImage(bitmap))
{
// Draw all the graphics using into g (into the bitmap)
g.DrawLine(Pens.Black, 0, 0, 100, 100);
}

// And maybe some control drawing if you want...?
this.label1.DrawToBitmap(bitmap, this.label1.Bounds);

ev.Graphics.DrawImage(bitmap, 0, 0);
}

0 pour la réponse № 2

En fait, la réponse de Yorye Nathan le 3 juin "12 à 7:33 est correct et c'est le point de départ qui m'a aidé. Cependant, je n'ai pas pu le faire fonctionner tel quel, j'ai donc apporté quelques corrections pour que cela fonctionne dans ma candidature. La correction consiste à extraire le format de page de l’imprimante du contexte de périphérique PrintPgeEventArgs.Graphics et à inclure les graphiques PrintPage en tant que troisième paramètre de la nouvelle construction Bitmap (...).

private void doc_PrintPage(object sender, PrintPageEventArgs ppea)
{
// Retrieve the physical bitmap boundaries from the PrintPage Graphics Device Context
IntPtr hdc = ppea.Graphics.GetHdc();
Int32 PhysicalWidth = GetDeviceCaps(hdc, (Int32)PHYSICALWIDTH);
Int32 PhysicalHeight = GetDeviceCaps(hdc, (Int32)PHYSICALHEIGHT);
ppea.Graphics.ReleaseHdc(hdc);

// Create a bitmap with PrintPage Graphic"s size and resolution
Bitmap myBitmap = new Bitmap(PhysicalWidth, PhysicalHeight, ppea.Graphics);
// Get the new work Graphics to use to draw the bitmap
Graphics myGraphics = Graphics.FromImage(myBitmap);

// Draw everything on myGraphics to build the bitmap

// Transfer the bitmap to the PrintPage Graphics
ppea.Graphics.DrawImage(myBitmap, 0, 0);

// Cleanup
myBitmap.Dispose();
}

////////
// Win32 API GetDeviceCaps() function needed to get the DC Physical Width and Height

const int PHYSICALWIDTH = 110;  // Physical Width in device units
const int PHYSICALHEIGHT = 111; // Physical Height in device units

// This function returns the device capability value specified
// by the requested index value.
[DllImport("GDI32.DLL", CharSet = CharSet.Auto, SetLastError = true)]
public static extern Int32 GetDeviceCaps(IntPtr hdc, Int32 nIndex);

Merci encore Yorye Nathan pour la réponse originale. //UN J