如果我检查流中的有​​效图像,则无法将字节写入服务器 - c#

我试图在将文件上传到图像服务器之前检查文件是否为图像。
我正在使用以下功能来完成此功能,该功能非常好:

static bool IsValidImage(Stream imageStream)
            {
                bool isValid = false;
                try
                {
                    // Read the image without validating image data
                    using (Image img = Image.FromStream(imageStream, false, false))
                    {
                        isValid = true;
                    }
                }
                catch
                {
                    ;
                }
                return isValid;
            }

问题是,随后立即调用以下代码时,该行:

while ((bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize)) > 0)

评估为零,不读取任何字节。我注意到当我移除
IsValidImage函数,读取字节并写入文件。它似乎
字节只能读取一次?任何想法如何解决这个问题?

using (FileStream outfile = new FileStream(filePath, FileMode.Create))
                    {
                        const int bufferSize = 65536; // 64K
                        int bytesRead = 0;

                        Byte[] buffer = new Byte[bufferSize];

                        while ((bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize)) > 0)
                        {
                            outfile.Write(buffer, 0, bytesRead);
                        }

                        outfile.Close(); //necessary?

                    }

更新:感谢您对Marc的帮助。我是流操作的新手,可以使用一点
这里有更多帮助。我拍摄了一下,但可能会混淆使用文件流和内存流。
您介意看看吗?再次感谢。

using (FileStream outfile = new FileStream(filePath, FileMode.Create))
using (MemoryStream ms = new MemoryStream())
{

   byte[] buffer = new byte[1024];
   int bytesRead;

   while ((bytesRead = request.FileByteStream.Read(buffer, 0, buffer.Length)) > 0)
   {
         ms.Write(buffer, 0, bytesRead);
   }

   // ms now has a seekable/rewindable copy of the data

   // TODO: read ms the first time

   // I replaced request.FileByteStream with ms but am unsure about 
   // the using statement in the IsValidImage function.

   if (!IsValidImage(ms) == true)
   {
        ms.Close();
        request.FileByteStream.Close();
        return;
   }

   ms.Position = 0;

   // TODO: read ms the second time

   byte[] m_buffer = new byte[ms.Length];
   while ((bytesRead = ms.Read(m_buffer, 0, (int)ms.Length)) > 0)
   {
        outfile.Write(m_buffer, 0, bytesRead);
   }


 } 


 static bool IsValidImage(MemoryStream imageStream)
 {
            bool isValid = false;
            try
            {
                // Read the image without validating image data
                using (Image img = Image.FromStream(imageStream, false, false))
                {
                    isValid = true;
                }
            }
            catch
            {
                ;
            }
            return isValid;
    }

参考方案

从任何流中读取时,位置都会增加。如果您读取流的末尾(通常是这样),然后尝试再次读取,则它将返回EOF。

对于某些流,您可以搜索-例如将Position设置为0。但是,您应该尝试避免依赖它,因为它不适用于许多流(尤其是涉及网络IO时)。您可以通过CanSeek来查询该功能,但是避免这种情况会更简单-部分地好像您是基于此分支的,您突然需要维护的代码量是原来的两倍。

如果需要两次数据,则选项取决于数据的大小。对于小型流,请将其作为byte[]MemoryStream缓冲在内存中。对于较大的流(或如果您不知道大小),则写入暂存文件(然后删除)是一种合理的方法。您可以随意打开和读取文件多次(以串行方式,而不是以并行方式)。

如果您感到满意,则流不会太大(尽管可以设置上限以防止人们上传交换文件等):

using (MemoryStream ms = new MemoryStream()) {
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0) {
        ms.Write(buffer, 0, bytesRead);
    }

    // ms now has a seekable/rewindable copy of the data

    // TODO: read ms the first time
    ms.Position = 0;
    // TODO: read ms the second time
}

ImageIO.read()是否考虑了EXIF方向元数据? - java

在JPEG图像中,有时会包含EXIF元数据,并告知应以什么方向显示图像。问题是,Java的ImageIO.read()在读取JPEG图像时是否将EXIF考虑在内,并自动应用转换。更具体地说,如果我使用Java的ImageIO将带有EXIF的JPEG图像转换为PNG图像,PNG图像的方向是否正确?还是下面的代码将在不考虑EXIF方向说明的情况下生成PNG图像?…

从iPython运行ga.read_ga问题 - python

从iPython Notebook运行代码时遇到了问题。这是运行的代码:import pandas.io.ga as ga import gflags df = ga.read_ga(['visits', 'avgTimeOnSite'], dimensions=['date', 'hou…

套接字InputStream块在available()/ read()上 - java

我正在阅读Socket InputStream,调用read()和available()可以进行几次循环迭代。以后的available()会无限期阻塞!可能是什么问题?我该如何使它不受阻碍?码:BufferedInputStream buffIn = new BufferedInputStream(in); while (true) { if (buffIn…

为什么我的方法在System.in.read()循环后被调用3次 - java

我已经开始学习Java,写了一些非常简单的东西,但是有一点我不理解:public static void main(String[] args) throws java.io.IOException { char ch; do { System.out.println("Quess the letter"); ch = (char) Sy…

给定不存在的文件时,python 3 configparser.read()不会引发异常 - python

当我尝试使用configparser.read读取不存在的文件时,我认为它应该引发异常。没有。而是返回一个空列表。显然,我可以测试一个空列表并引发异常。在我看来,如果configparser.read引发FileNotFound异常会更直观,更安全。jeffs@jeffs-laptop:~/nbmdt (blue-sky)*$ python3.6 Pytho…