このコードの目的は、指定された場所から不明な番号の.JPGを取得し、「Microsoft Print to PDF」に送信することです。約5枚目の.jpgイメージの後、Image img = imgs[index];
で「メモリ不足」の例外が発生します。これはどのように対処できますか?C#メモリ不足例外 - イメージタイプ
更新日:2016年12月11日@ 21:00(この問題は、次のコードで解決されている)
あなたのコードで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
:よう
何か? – agfc
'BuildImageList'で読み込もうとしている画像の数はいくつですか? – apocalypse
関連するすべてのコードを含むように更新しました。画像の数については、この数字は不明です。 5にすることも、100にすることもできます。 – Tennis