【问题标题】:Mono stream issue单声道流问题
【发布时间】:2015-01-17 00:35:50
【问题描述】:

我想使用 NancyFX 在服务器上上传文件。我这样做:

var fileStream = File.Create(path_to_file);
file.Value.Seek(0, SeekOrigin.Begin);
file.Value.CopyTo(fileStream);
fileStream.Close();

我也试过了:

byte[] buffer;
using (var memoryStream = new MemoryStream())
{
   file.Value.Seek(0, SeekOrigin.Begin);
   file.Value.CopyTo(memoryStream);
   buffer = memoryStream.ToArray();
}

MemoryStream ms = new MemoryStream(buffer, 0, buffer.Length);
ms.Position = 0;
Image img = Image.FromStream(ms, true);
img.Save(path);

还有这个:

File.WriteAllBytes(coverPath, buffer);

在我的 Windows PC 上,它运行良好。在 Linux 服务器上,图像正在保存,但图像内容与原始图像不匹配。 所以,这里是图片: 原图,即上传中:http://i.stack.imgur.com/mTsE2.jpg 已保存图片:http://i.stack.imgur.com/ORKn1.jpg 如果图像大小

【问题讨论】:

    标签: linux image stream mono nancy


    【解决方案1】:

    据我了解,您有一个 fileStream 并且您想将其保存为 Image

    这里的代码在带有单 JIT 编译器版本 2.10.8.1 的 Windows 8 .net 4.5 和 Ubuntu 12 TLS 中运行良好

    class Program
    {
        static void Main(string[] args)
        {
            string fileName = @"img/mTsE2.jpg";
            string destimationImage = @"img/dest.jpg";
    
            string appPath = AppDomain.CurrentDomain.BaseDirectory;
    
            string pathToFile = Path.Combine(appPath, fileName);
    
            if (!File.Exists(pathToFile))
            {
                Console.WriteLine("Cant find file {0}", pathToFile);
                Console.ReadLine();
                return;
            }
    
            MemoryStream memoryStream = new MemoryStream();
    
    
    
    
            using (FileStream fileStream = File.Open(pathToFile, FileMode.Open))
            {
                Console.WriteLine("Source length: {0}", fileStream.Length);
                byte[] bytes = new byte[fileStream.Length];
    
                fileStream.Read(bytes, 0, (int)fileStream.Length);
    
                memoryStream.Write(bytes, 0, (int)fileStream.Length);
            }
    
            Console.WriteLine("memoryStream length {0}", memoryStream.Length);
    
            string destPath = Path.Combine(appPath, destimationImage);
            CopyAsImage(destPath, memoryStream);
    
            Console.WriteLine("Done! Check file {0}",destPath);
            Console.ReadLine();
        }
    
        private static void CopyAsImage(string fileName, Stream stream)
        {
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            Image image = Image.FromStream(stream, true, true);
            image.Save(fileName);
    
    
        }
    }
    

    如果它仍然不起作用,请注意尺寸。实际上 int 是一个 Int32 ,对于最多 2+Gigs 的字节数组应该没问题

    【讨论】:

    • 我无法初始化 fileStream,因为我已经有来自 nancy 请求的文件流。
    • using (Stream stream = file.Value) 所以,出现下一个错误:Nancy.RequestExecutionException: Oh noes! ---> System.ArgumentException: A null reference or invalid value was found [GDI+ status: InvalidParameter] at System.Drawing.GDIPlus.CheckStatus (System.Drawing.Status) <0x00157> at System.Drawing.Image.CreateFromHandle (intptr) <0x00027> at System.Drawing.Image.LoadFromStream (System.IO.Stream,bool) <0x0002b> at System.Drawing.Image.FromStream (System.IO.Stream,bool,bool) <0x00013>
    • 这里抛出异常:Image image = Image.FromStream(stream, true, true); in CopyAsImage 方法
    • 您运行的是哪个版本的单声道? mcs 编译选项的参数是什么?奇怪的是,在我的本地(参见上面的规范)上它运行正常。你试过在你的linux环境上运行上面的例子吗?
    • Strange 在我的机器上运行良好,在 Windows 上编译并在 ubuntu 上运行。你真的需要它是图像,你是在加载后更改它还是只需要保存它。愿此链接对blogs.lessthandot.com/index.php/webdev/serverprogramming/… 有所帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 2012-09-13
    • 1970-01-01
    相关资源
    最近更新 更多