此代码的目的是从指定位置获取未知号码.JPG,并将其发送到“Microsoft Print to PDF”。在大约第5张.jpg图像之后,它会在Image img = imgs[index];
处抛出“内存不足”异常。我该如何解决这个问题?
更新时间:2016-12-11 @ 9:00 PM (此问题已通过以下代码解决)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;
using System.Diagnostics;
using System.Deployment.Application;
namespace CEB_Process
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Texbox Input
string Case_No;
int index = 0;
private static Image[] _imgs;
//==========================================================================================================
//CONVERT PICTURES TO PDF Button Click
//==========================================================================================================
private void button2_Click(object sender, EventArgs e)
{
// set the directory to store the output.
string newdirectory = string.Format(@"C:\{0}", Case_No);
// generate a file name as the current date/time in unix timestamp format
string newFileName = "5 x 7 in";
try
{
// initialize PrinterDocument object
PrintDocument pd = new PrintDocument()
{
//Printer Settings
PrinterSettings = new PrinterSettings()
{
// set the printer to 'Microsoft Print to PDF'
PrinterName = "Microsoft Print to PDF",
// tell the object this document will print to file
PrintToFile = true,
// set the filename to whatever you like (full path)
PrintFileName = Path.Combine(newdirectory, newFileName + ".pdf"),
}//End Printer settings
};//End PrintDocument()
Page_Init(null, new EventArgs());
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
}//End try
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}//End Button Module
public static void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
BuildImageList();
}
//===========================================
// Print Event Handler
//===========================================
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
Graphics graphic = ev.Graphics;
Point p = new Point(10, 10);
Image img = _imgs[index];
graphic.DrawImage(img, p);
index++;
ev.HasMorePages = index < _imgs.Length;
img.Dispose();
}
//===============================================
// Get Build the Image List
//===============================================
public static void BuildImageList()
{
string sdira = @"C:\test";
_imgs = System.IO.Directory.GetFiles(sdira, "*.JPG").Select(f => Image.FromFile(f)).ToArray();
}
}//End Public Class
}//End Namespace
答案 0 :(得分:3)
在您的代码中,您似乎在构建图像阵列的次数与您调用 Print_Page 的次数相同,这可能是您的问题所在。你只需要这样做一次。
让你的形象[] imgs;本地字段或属性,并且每个映像批次只调用 BuildImageList 一次(每页加载或您的环境)。
类似的东西:
private static Image[] _imgs;
//==========================================================================================================
//CONVERT PICTURES TO PDF Button Click
//==========================================================================================================
private void button2_Click(object sender, EventArgs e)
{
// set the directory to store the output.
string newdirectory = string.Format(@"C:\{0}", Case_No);
// generate a file name as the current date/time in unix timestamp format
string newFileName = "5 x 7 in";
try
{
// initialize PrinterDocument object
PrintDocument pd = new PrintDocument()
{
//Printer Settings
PrinterSettings = new PrinterSettings()
{
// set the printer to 'Microsoft Print to PDF'
PrinterName = "Microsoft Print to PDF",
// tell the object this document will print to file
PrintToFile = true,
// set the filename to whatever you like (full path)
PrintFileName = Path.Combine(newdirectory, newFileName + ".pdf"),
}//End Printer settings
};//End PrintDocument()
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
}//End try
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}//End Button Module
public static void Page_Init(object sender, EventArgs e)
{
if(!IsPostBack)
BuildImageList();
}
//===========================================
// Print Event Handler
//===========================================
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
Graphics graphic = ev.Graphics;
Point p = new Point(10, 10);
Image img = _imgs[index];
graphic.DrawImage(img, p);
index++;
ev.HasMorePages = index < _imgs.Length;
img.Dispose();
}
//===============================================
// Get Build the Image List
//===============================================
public static void BuildImageList()
{
String sdira = @"C:\test";
_imgs = System.IO.Directory.GetFiles(sdira, "*.JPG").Select(f => Image.FromFile(f)).ToArray();
}