可以使用抓取图片的方法,使用API BITBLT ,把GRID 的图形变成图形文件, 然后利用PrintDocument 控件的ON_Paint()事件里面把图片打印出来!。 或者直接在ON_Paint事件里用绘制网格和文字这样做比较麻烦! 附代码: 图形抓取类: /// <summary> /// Summary description for CCaptueImage. /// </summary> public class CCaptureImage { #region API DECLARE [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")] private static extern bool BitBlt( IntPtr hdcDest, // handle to destination DC int nXDest, // x-coord of destination upper-left corner int nYDest, // y-coord of destination upper-left corner int nWidth, // width of destination rectangle int nHeight, // height of destination rectangle IntPtr hdcSrc, // handle to source DC int nXSrc, // x-coordinate of source upper-left corner int nYSrc, // y-coordinate of source upper-left corner System.Int32 dwRop // raster operation code ); #endregion
public CCaptureImage() { // // TODO: Add constructor logic here // }
#region My Property ,Field private int mImgWidth=400; /// <summary> /// Capture image's width /// </summary> public int ImgWidth { set{mImgWidth=value;} get{return mImgWidth;} } private int mImgHeight=300; /// <summary> /// Capture image's height /// </summary> public int ImgHeight { set{mImgHeight=value;} get{return mImgHeight;} } public int mCapLeft=0; /// <summary> /// Capture image left position of screen coordinate /// </summary> public int CapLeft { set{mCapLeft=value;} get{return mCapLeft;} } public int mCapTop=0; /// <summary> /// Capture image top position of screen coordinate /// </summary> public int CapTop { set{mCapTop=value;} get{return mCapTop;} } private Form mSelectForm; /// <summary> /// Capture image's form /// </summary> public Form SelectForm { set{mSelectForm=value;} get{return mSelectForm;} }
#endregion #region Functions /// <summary> /// Capture image function /// </summary> /// <param name="filename">the path of the file</param> /// <returns>true is scuessfully,false is failure</returns> public bool SaveImage(string filename) {
if(MessageBox.Show("È·¶¨Òª´òÓ¡£¿", "´òÓ¡", MessageBoxButtons.YesNo , MessageBoxIcon.Information ) ==DialogResult.Yes ) { pntPreview.ShowDialog (); } } catch(Exception err) { MessageBox.Show (err.Message ,"´íÎó", MessageBoxButtons.OK , MessageBoxIcon.Information ); } } private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { try { //initialize print setting; int pntW=pntDoc.DefaultPageSettings.Bounds.Width ; int pntH=pntDoc.DefaultPageSettings.Bounds.Height; //draw Title int X=0; int Y=5;
Graphics g=e.Graphics ;
// Image ImgTitle=Image.FromFile (Directory.GetCurrentDirectory() + // "//Parm//Bmp//Title.bmp"); Rectangle DRect=new Rectangle (new Point (0,0), new Size (pntW,pntH)); Rectangle SRect=new Rectangle (new Point (0,0), new Size (pntW,pntH));
//g.DrawImage (ImgTitle,DRect,SRect,GraphicsUnit.Pixel); Font TitleFt=new Font ("Arial",16); //draw title g.DrawString ("Title- T6800 View",TitleFt,new SolidBrush (Color.Black ),new PointF (X,Y+90)); //draw date g.DrawString ("Date -"+DateTime.Now.Date.ToShortDateString() ,TitleFt, new SolidBrush (Color.Black ), new PointF (X,Y+110)); //draw copys g.DrawString ("Copys-"+pntDoc.PrinterSettings.Copies.ToString ("00"),TitleFt, new SolidBrush (Color.Black ), new PointF (X,Y+130)); //draw a line g.DrawLine (new Pen (new SolidBrush (Color.Black ),1.0f), new Point (X,Y+160), new Point (pntW,Y+160)); Image Img=Image.FromFile(Directory.GetCurrentDirectory () + "\\temp.bmp"); //draw image Point spt=new Point (0,0); Point dpt=new Point (pntW/2-Img.Width /2,200); Rectangle SrcRect=new Rectangle (spt,new Size (pntW,pntH)); Rectangle DesRect=new Rectangle (dpt,new Size (pntW,pntH)); g.DrawImage(Img,DesRect,SrcRect,GraphicsUnit.Pixel); } catch(Exception err) { MessageBox.Show (err.Message ,"´íÎó", MessageBoxButtons.OK , MessageBoxIcon.Information ); } }
给我发个邮件,我把例子发给你
[email protected]
然后利用PrintDocument
控件的ON_Paint()事件里面把图片打印出来!。
或者直接在ON_Paint事件里用绘制网格和文字这样做比较麻烦!
附代码:
图形抓取类:
/// <summary>
/// Summary description for CCaptueImage.
/// </summary>
public class CCaptureImage
{ #region API DECLARE
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);
#endregion
public CCaptureImage()
{
//
// TODO: Add constructor logic here
//
}
#region My Property ,Field private int mImgWidth=400;
/// <summary>
/// Capture image's width
/// </summary>
public int ImgWidth
{
set{mImgWidth=value;}
get{return mImgWidth;}
}
private int mImgHeight=300;
/// <summary>
/// Capture image's height
/// </summary>
public int ImgHeight
{
set{mImgHeight=value;}
get{return mImgHeight;}
}
public int mCapLeft=0;
/// <summary>
/// Capture image left position of screen coordinate
/// </summary>
public int CapLeft
{
set{mCapLeft=value;}
get{return mCapLeft;}
}
public int mCapTop=0;
/// <summary>
/// Capture image top position of screen coordinate
/// </summary>
public int CapTop
{
set{mCapTop=value;}
get{return mCapTop;}
}
private Form mSelectForm;
/// <summary>
/// Capture image's form
/// </summary>
public Form SelectForm
{
set{mSelectForm=value;}
get{return mSelectForm;}
}
#endregion #region Functions
/// <summary>
/// Capture image function
/// </summary>
/// <param name="filename">the path of the file</param>
/// <returns>true is scuessfully,false is failure</returns>
public bool SaveImage(string filename)
{
try
{
if (SelectForm==null)
{
MessageBox.Show ("Not capture image form","No form",
MessageBoxButtons.OK ,
MessageBoxIcon.Information );
return false;
}
Graphics g1 =SelectForm.CreateGraphics ();
Image CapImg=new Bitmap (ImgWidth,ImgHeight,g1);
Graphics g2=Graphics.FromImage (CapImg);
IntPtr dc1=g1.GetHdc ();
IntPtr dc2=g2.GetHdc ();
BitBlt(dc2,0,0,ImgWidth,ImgHeight,dc1,CapLeft,CapTop,13369376);
g1.ReleaseHdc(dc1);
g2.ReleaseHdc (dc2);
CapImg.Save (filename);
return true;
}
catch
{
MessageBox.Show ("Capture image failure!","failure",
MessageBoxButtons.OK ,
MessageBoxIcon.Information);
return false;
}
}
#endregion }打印代码
private void btnGraphPrint_Click(object sender, System.EventArgs e)
{
try
{
string imgfilename=Directory.GetCurrentDirectory ()+"\\temp.bmp";
CCaptureImage ci=new CCaptureImage ();
ci.SelectForm =this;
ci.CapLeft =pnlGraphView.Left+ttChart2.Left;
ci.CapTop =pnlGraphView.Top+ttChart2.Top;
ci.ImgHeight =ttChart2.Height;
ci.ImgWidth =ttChart2.Width;
this.temI550N1.TimeDelayFunc(500);
ci.SaveImage (imgfilename);
if(MessageBox.Show("È·¶¨Òª´òÓ¡£¿",
"´òÓ¡",
MessageBoxButtons.YesNo ,
MessageBoxIcon.Information )
==DialogResult.Yes )
{
pntPreview.ShowDialog ();
}
}
catch(Exception err)
{
MessageBox.Show (err.Message ,"´íÎó",
MessageBoxButtons.OK ,
MessageBoxIcon.Information );
}
}
private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
try
{
//initialize print setting;
int pntW=pntDoc.DefaultPageSettings.Bounds.Width ;
int pntH=pntDoc.DefaultPageSettings.Bounds.Height;
//draw Title
int X=0;
int Y=5;
Graphics g=e.Graphics ;
// Image ImgTitle=Image.FromFile (Directory.GetCurrentDirectory() +
// "//Parm//Bmp//Title.bmp");
Rectangle DRect=new Rectangle (new Point (0,0),
new Size (pntW,pntH));
Rectangle SRect=new Rectangle (new Point (0,0),
new Size (pntW,pntH));
//g.DrawImage (ImgTitle,DRect,SRect,GraphicsUnit.Pixel);
Font TitleFt=new Font ("Arial",16);
//draw title
g.DrawString ("Title- T6800 View",TitleFt,new SolidBrush (Color.Black ),new PointF (X,Y+90));
//draw date
g.DrawString ("Date -"+DateTime.Now.Date.ToShortDateString() ,TitleFt,
new SolidBrush (Color.Black ),
new PointF (X,Y+110));
//draw copys
g.DrawString ("Copys-"+pntDoc.PrinterSettings.Copies.ToString ("00"),TitleFt,
new SolidBrush (Color.Black ),
new PointF (X,Y+130));
//draw a line
g.DrawLine (new Pen (new SolidBrush (Color.Black ),1.0f),
new Point (X,Y+160),
new Point (pntW,Y+160));
Image Img=Image.FromFile(Directory.GetCurrentDirectory () +
"\\temp.bmp");
//draw image
Point spt=new Point (0,0);
Point dpt=new Point (pntW/2-Img.Width /2,200);
Rectangle SrcRect=new Rectangle (spt,new Size (pntW,pntH));
Rectangle DesRect=new Rectangle (dpt,new Size (pntW,pntH));
g.DrawImage(Img,DesRect,SrcRect,GraphicsUnit.Pixel);
}
catch(Exception err)
{
MessageBox.Show (err.Message ,"´íÎó",
MessageBoxButtons.OK ,
MessageBoxIcon.Information );
}
}
不是在声明graphics 实例时,将设备打印机就可以了吗?
兄弟有提供一下例子吗?
{drawline...
draw.....
draw....
}