以下代码用于拼接现有PDF [另外我们使用TallComponents进行实际拼接,以防您想知道PDFUtility是什么]:
PDFUtility.Document docFinal = new PDFUtility.Document();
PDFUtility.Document docToAdd = null;
byte[] combinedFile;
foreach (byte[] content in fileContents)
{
    MemoryStream fileContentStream = new MemoryStream(content);
    docToAdd = new PDFUtility.Document(fileContentStream);
    docFinal.Pages.AddRange(docToAdd.Pages.CloneToArray());
}
using (MemoryStream stream = new MemoryStream())
{
    docFinal.Write(stream);
    combinedFile = stream.ToArray();
}
这段代码的明显问题是这个命令:
MemoryStream fileContentStream = new MemoryStream(content);
内存流fileContentStream没有被处理掉,可能(我相信)保持资源的时间超过了所需的时间.
显而易见的解决方案是将MemoryStream的创建包装在一个using块中.代码将如下所示:
PDFUtility.Document docFinal = new PDFUtility.Document();
PDFUtility.Document docToAdd = null;
byte[] combinedFile;
foreach (byte[] content in fileContents)
{
    using (MemoryStream stream = new MemoryStream())
    { …我有这个有效的代码:
public async Task<IActionResult> OnGet()
{
    var workbook = GenerateClosedXMLWorkbook();
    MemoryStream memoryStream = new MemoryStream();
    workbook.SaveAs(memoryStream);
    memoryStream.Position = 0;
    return File(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "hello.xlsx");
    }
private XLWorkbook GenerateClosedXMLWorkbook()
{
    var workbook = new XLWorkbook();
    var worksheet = workbook.Worksheets.Add("Sample Sheet");
    worksheet.Cell("A1").Value = "Hello World!";
    worksheet.Cell("A2").FormulaA1 = "=MID(A1, 7, 5)";
    return workbook;
}
但是,在我看来,我需要以某种方式关闭或处理 memoryStream。那是对的吗?还是会自动关闭?
我是对的,如果你正在做的话,你只需要一个using()用于最外层的流
MemoryStream mstr = new MemoryStream();
using(StreamWriter w = new StreamWriter(mstr)) {
    ....
}
由于处理StreamWriter还应该处理/关闭底层流,没有必要这样做?:
using(MemoryStream mstr = new MemoryStream())
using(StreamWriter w = new StreamWriter(mstr)) {
    ....
}
(注意这些只是示例,关于如何处理包装的流,而不是寻找像使用StringWriter等的替代方案)
我必须从许多图像中导入大量的图像裁剪,这些图像都已准备好存储在我的数据库中.我每次尝试使用语句并处理我的位图对象.但我仍然得到一个内存溢出异常,我的系统内存不足.
以下是我正在做的一些示例代码.
public void CropImage(List<ImageClass> data)
{
    foreach (var obj in data)
    {
        //I have a data base method that returns a data object that 
        //contains the file bytes of the image id in data: 'file'
        //My List<ImageClass> data contains an ID of the original image
        //start x,y coords for the upper left corner of the rectangle,
        //and the width and height of the rectangle.
        Image img = Image.FromStream(new MemoryStream(file.Data));
        Bitmap bmp = new Bitmap((Bitmap)img);
        Rectangle cropArea = new Rectangle(obj.x_coordinate, …我正在开发一个图像处理程序,我需要从视频中保存一些图片并对它们进行一些处理.当1张图片处理它并没有真正花时间,但是当我在处理100张图片这让我保存文件到我的硬盘的区别,这就是为什么它需要时间的事情是,我的功能m using是一个现成的函数,它只接受(文件名)函数真的很复杂,所以我不能建立自己的函数(如果这是你在想的)
我现在正在考虑两件事,并希望得到你的意见:
感谢您的帮助,非常感谢
这是我的代码,但我仍有问题:
            Capture video = new Capture("c:\\5.avi");
            Image<Bgr, Byte> Imageframe ;
            Dictionary<string, MemoryStream> dict = new Dictionary<string, MemoryStream>();
                Imageframe = video.QueryFrame();
                Bitmap bmp = Imageframe.ToBitmap();
                dict.Add("mypicture.png", new MemoryStream());
                bmp.Save(dict["mypicture.png"],imageformat.png);    
它说的图像格式在上下文中不存在
这是我使用的函数:
Image<Bgr, byte> result;
                 result = DrawMatches.Draw("box.png", "box_in_scene.png", out matchTime,i);