相关疑难解决方法(0)

显式归零

在什么情况下java中显式nulling有用.它是否以任何方式通过使对象无法访问或某些东西来帮助垃圾收集器?它被认为是一种好习惯吗?

java

9
推荐指数
4
解决办法
2021
查看次数

如何处置MemoryStream对象

以下代码用于拼接现有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();
}
Run Code Online (Sandbox Code Playgroud)

这段代码的明显问题是这个命令:

MemoryStream fileContentStream = new MemoryStream(content);
Run Code Online (Sandbox Code Playgroud)

内存流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())
    { …
Run Code Online (Sandbox Code Playgroud)

.net c# dispose memorystream

9
推荐指数
1
解决办法
1371
查看次数

我需要关闭这个内存流吗?

我有这个有效的代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

但是,在我看来,我需要以某种方式关闭或处理 memoryStream。那是对的吗?还是会自动关闭?

c# asp.net-core asp.net-core-2.0

4
推荐指数
1
解决办法
1537
查看次数

使用()并使用多个包装流进行处理

我是对的,如果你正在做的话,你只需要一个using()用于最外层的流

MemoryStream mstr = new MemoryStream();

using(StreamWriter w = new StreamWriter(mstr)) {
    ....
}
Run Code Online (Sandbox Code Playgroud)

由于处理StreamWriter还应该处理/关闭底层流,没有必要这样做?:

using(MemoryStream mstr = new MemoryStream())
using(StreamWriter w = new StreamWriter(mstr)) {
    ....
}
Run Code Online (Sandbox Code Playgroud)

(注意这些只是示例,关于如何处理包装的流,而不是寻找像使用StringWriter等的替代方案)

c# dispose stream

3
推荐指数
2
解决办法
3490
查看次数

在循环中创建位图时出现内存溢出异常

我必须从许多图像中导入大量的图像裁剪,这些图像都已准备好存储在我的数据库中.我每次尝试使用语句并处理我的位图对象.但我仍然得到一个内存溢出异常,我的系统内存不足.

以下是我正在做的一些示例代码.

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, …
Run Code Online (Sandbox Code Playgroud)

c# memory imaging bitmap overflow

1
推荐指数
1
解决办法
1625
查看次数

c#将图片文件保存到ram中

我正在开发一个图像处理程序,我需要从视频中保存一些图片并对它们进行一些处理.当1张图片处理它并没有真正花时间,但是当我在处理100张图片这让我保存文件到我的硬盘的区别,这就是为什么它需要时间的事情是,我的功能m using是一个现成的函数,它只接受(文件名)函数真的很复杂,所以我不能建立自己的函数(如果这是你在想的)

我现在正在考虑两件事,并希望得到你的意见:

  1. 改变功能的输入,但如何?有没有办法将此输入从(文件名)更改为包含这些图片的数组?
  2. 将文件保存到ram.但如何通过名称将文件保存到ram,并能够在函数中将它们用作(文件名)?

感谢您的帮助,非常感谢

这是我的代码,但我仍有问题:

            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);    
Run Code Online (Sandbox Code Playgroud)

它说的图像格式在上下文中不存在

这是我使用的函数:

Image<Bgr, byte> result;
                 result = DrawMatches.Draw("box.png", "box_in_scene.png", out matchTime,i); 
Run Code Online (Sandbox Code Playgroud)

c# ram image file

1
推荐指数
1
解决办法
2794
查看次数