winform 如何遍历目录下所有JPG\PNG格式的图片并逐张打印?
2个回答
展开全部
分为三步:
拖一个打印控件,添加打印事件
遍历目录,获取所有以JPG和PNG为后缀名的文件路径数组
开始打印,设置打印附加页,一次性打印完
下面是代码:
private string[] m_FileList; //存储图片的数组
private int i = 0;//记录开始
private void btn_begin_Click(object sender, EventArgs e)
{
string l_strFileDir = "";//你自己的路径
FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.ShowNewFolderButton = false;
dlg.Description = "选择图片文件夹";
if (dlg.ShowDialog() == DialogResult.OK)
{
l_strFileDir = dlg.SelectedPath;
}
else
{
return;
}
//获取该目录下所有的jpg和png图片 没有测试大小写
string[] FileList = Directory.GetFiles(l_strFileDir, "*.jpg", SearchOption.AllDirectories);
string[] FileList1 = Directory.GetFiles(l_strFileDir, "*.PNG", SearchOption.AllDirectories);
m_FileList = FileList.Concat(FileList1).ToArray();
PrintDialog MyPrintDg = new PrintDialog();
MyPrintDg.Document = printDocument1;
MyPrintDg.AllowSomePages = true;
if (MyPrintDg.ShowDialog() == DialogResult.OK)
{
try
{
printDocument1.Print();
}
catch
{ //停止打印
printDocument1.PrintController.OnEndPrint(printDocument1, new System.Drawing.Printing.PrintEventArgs());
}
}
}
private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
try
{
if (i < m_FileList.Length)
{
e.Graphics.DrawImageUnscaled(Image.FromFile(m_FileList[i]), e.PageBounds);
e.HasMorePages = true; //设置是否继续打印附加页面
i++;
if (i == m_FileList.Length)
{
e.HasMorePages = false;
}
}
//e.Graphics.DrawImage(pictureBox1.Image, 20, 20);
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
展开全部
给你提供一个文件夹遍历图片的方法,打印自己找找C#有没有什么api
//图片过滤器
public string PictureFilter = ".bmp;.BMP;.jpg;.JPG;.jpeg;JPEG;.gif;.GIF;.png;.PNG;";
//递归遍历目录下所有图片文件的列表
public static void GetFileNames(string pathname, List<string> fileList)
{
string[] subFiles = Directory.GetFiles(pathname);
foreach (string subFile in subFiles)
{
if (Util.IsValidFile(subFile, App.iCommCfg.PictureFilter))
{
fileList.Add(subFile);
Console.WriteLine(subFile);
}
}
string[] subDirs = Directory.GetDirectories(pathname);
foreach (string subDir in subDirs)
{
GetFileNames(subDir, fileList);
}
}
//图片过滤器
public string PictureFilter = ".bmp;.BMP;.jpg;.JPG;.jpeg;JPEG;.gif;.GIF;.png;.PNG;";
//递归遍历目录下所有图片文件的列表
public static void GetFileNames(string pathname, List<string> fileList)
{
string[] subFiles = Directory.GetFiles(pathname);
foreach (string subFile in subFiles)
{
if (Util.IsValidFile(subFile, App.iCommCfg.PictureFilter))
{
fileList.Add(subFile);
Console.WriteLine(subFile);
}
}
string[] subDirs = Directory.GetDirectories(pathname);
foreach (string subDir in subDirs)
{
GetFileNames(subDir, fileList);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |