c#out memory异常 - 图像类型



此代码的目的是从指定的位置获取一个未知号。大约第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

在您的代码中,似乎您正在构建一个图像数组,就像您调用 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();
    }

最新更新