C# winform如何打印图片?求指点
解决方案 »
- 求助c# 怎样截取小数位数?
- Myqq 文本编辑器
- 各位已在工作了的朋友,在做项目开发时,平时都应该注意一些什么?非常感谢能给我们即将毕业的学生提醒一下!
- c# 初学者问 界面设计: 谁能设计这样的界面?告诉我用那个控件
- “命名空间“System”中不存在类型或命名空间名称“Windows”(是缺少程序集引用吗?)”怎么解决呀?
- showPopup位置怎么定位?
- C#中怎么使用打印机打印报表?
- c# (winform中)的DataGrid1怎么不能DataGrid1.DataBind();进行绑定啊?
- silverlight 设定grid大小
- C# 怎样自定义文件删除对话框?
- VS2008,調試時無法進入Dao層里面
- 一个C# 调用OUTLOOK的问题。
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控件