2016-12-10 20 views
-2

このコードの目的は、指定された場所から不明な番号の.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 
+0

:よう

何か? – agfc

+0

'BuildImageList'で読み込もうとしている画像の数はいくつですか? – apocalypse

+0

関連するすべてのコードを含むように更新しました。画像の数については、この数字は不明です。 5にすることも、100にすることもできます。 – Tennis

答えて

3

あなたがイメージアレイなどの構築しているようです何度も電話しているので、Print_Pageこれは問題の可能性があります。あなたは一度だけこれを行う必要があります。

Image [] imgsを作成します。ローカルフィールドまたはプロパティを使用し、BuildImageListを1回のイメージバッチで1回だけ(ページごとの読み込みまたは環境に関係なく)呼び出します。

からあなたpd_PrintPage呼び出されている
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(); 
    } 
+0

すべての画像を一度に読み込むことはできません。より良い解決策は、まずjpegファイルへのすべてのパスを取得することです。その後、イベントハンドラで、彼はロードし、描画し、予想されるイメージを破棄する必要があります。 – apocalypse

+0

agfcの提案を反映するために、元のコードを上に更新しました。しかし、私はそれをコンパイルすることはできません:( "非静的フィールドのオブジェクト参照が必要です..." _imgs = System.IO.Directory.GetFiles(sdira、 "* .JPG")。Select(f = > Image.FromFile(f))。ToArray(); ' – Tennis

+0

これは動作するはずです。静的であるか、最初に使用する前に初期化する必要があります。 – agfc