c# 打印图片 要怎么弄啊 请高手赐教!
1个回答
展开全部
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]//应用API函数
private static extern bool BitBlt(
IntPtr hdcDest, // 目标设备的句柄
int nXDest, // 目标对象的左上角的X坐标
int nYDest, // 目标对象的左上角的X坐标
int nWidth, // 目标对象的矩形的宽度
int nHeight, // 目标对象的矩形的长度
IntPtr hdcSrc, // 源设备的句柄
int nXSrc, // 源对象的左上角的X坐标
int nYSrc, // 源对象的左上角的X坐标
System.Int32 dwRop // 光栅的操作值
);
private void 打印照片列表ToolStripMenuItem_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(1024,1100, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
//新建绘制到bmp
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);//上底色
g.DrawString(cmbJS.Text, this.Font,Brushes.Black, new PointF(975, 1950));
int i = 0, Y = 50, X = 79;
foreach (Control c in panel4.Controls) //这个地方遍历图片
{
if (c is PictureBox)//发现是图片
{
if (i % 4 == 0)// 因为我要打印多张 所以这个地方我要 4张图片一行// 如果你只打印一张 此处不需要
{
Y = 50 + 170 * (i / 4);
X = 79;
}
else
{
X =79 + (i % 4) * 170;
}
i++;
PictureBox pb = (PictureBox)c;
//绘制包含的图象
g.DrawImage(pb.Image,X,Y,120,160);
}
}
//panel1外的pictureBox输出,要保存写成bmp.Save(路径,格式);
g.Dispose();
Graphics graphic = panel4.CreateGraphics();
Size s = panel4.Size;
// memImage = new Bitmap(s.Width,s.Height, graphic);
memImage = new Bitmap(s.Width,1024, graphic);
Graphics memGraphic = Graphics.FromImage(memImage);
IntPtr dc1 = graphic.GetHdc();
IntPtr dc2 = memGraphic.GetHdc();
// BitBlt(dc2, 0, 0, panel4.ClientRectangle.Width, panel4.ClientRectangle.Height,dc1, 0, 0, 13369376);
BitBlt(dc2, 0, 0, panel4.ClientRectangle.Width, 1024, dc1, 0, 0, 13369376);
//Clone the bitmap so we can dispose it.
print_image = (Image)memImage.Clone();
graphic.ReleaseHdc(dc1);
memGraphic.ReleaseHdc(dc2);
graphic.Dispose();
memGraphic.Dispose();
memImage.Dispose();
print_image = (Image)bmp.Clone();
PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Width = 800;
dlg.Height = 600;
dlg.Document = printDocument1;
if (dlg.ShowDialog() == DialogResult.OK)
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int Row = 100;
e.Graphics.DrawImage(print_image, 0, 0);
}
你试试看吧
需要引用
using System.Runtime.InteropServices;
和添加printDocument控件
private static extern bool BitBlt(
IntPtr hdcDest, // 目标设备的句柄
int nXDest, // 目标对象的左上角的X坐标
int nYDest, // 目标对象的左上角的X坐标
int nWidth, // 目标对象的矩形的宽度
int nHeight, // 目标对象的矩形的长度
IntPtr hdcSrc, // 源设备的句柄
int nXSrc, // 源对象的左上角的X坐标
int nYSrc, // 源对象的左上角的X坐标
System.Int32 dwRop // 光栅的操作值
);
private void 打印照片列表ToolStripMenuItem_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(1024,1100, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
//新建绘制到bmp
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);//上底色
g.DrawString(cmbJS.Text, this.Font,Brushes.Black, new PointF(975, 1950));
int i = 0, Y = 50, X = 79;
foreach (Control c in panel4.Controls) //这个地方遍历图片
{
if (c is PictureBox)//发现是图片
{
if (i % 4 == 0)// 因为我要打印多张 所以这个地方我要 4张图片一行// 如果你只打印一张 此处不需要
{
Y = 50 + 170 * (i / 4);
X = 79;
}
else
{
X =79 + (i % 4) * 170;
}
i++;
PictureBox pb = (PictureBox)c;
//绘制包含的图象
g.DrawImage(pb.Image,X,Y,120,160);
}
}
//panel1外的pictureBox输出,要保存写成bmp.Save(路径,格式);
g.Dispose();
Graphics graphic = panel4.CreateGraphics();
Size s = panel4.Size;
// memImage = new Bitmap(s.Width,s.Height, graphic);
memImage = new Bitmap(s.Width,1024, graphic);
Graphics memGraphic = Graphics.FromImage(memImage);
IntPtr dc1 = graphic.GetHdc();
IntPtr dc2 = memGraphic.GetHdc();
// BitBlt(dc2, 0, 0, panel4.ClientRectangle.Width, panel4.ClientRectangle.Height,dc1, 0, 0, 13369376);
BitBlt(dc2, 0, 0, panel4.ClientRectangle.Width, 1024, dc1, 0, 0, 13369376);
//Clone the bitmap so we can dispose it.
print_image = (Image)memImage.Clone();
graphic.ReleaseHdc(dc1);
memGraphic.ReleaseHdc(dc2);
graphic.Dispose();
memGraphic.Dispose();
memImage.Dispose();
print_image = (Image)bmp.Clone();
PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Width = 800;
dlg.Height = 600;
dlg.Document = printDocument1;
if (dlg.ShowDialog() == DialogResult.OK)
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int Row = 100;
e.Graphics.DrawImage(print_image, 0, 0);
}
你试试看吧
需要引用
using System.Runtime.InteropServices;
和添加printDocument控件
追问
你的那个按钮事件是给谁加的啊?!
追答
是一个button 的点击事件
当点击的时候 就预览
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询