linux - 单声道流问题

标签 linux image stream mono nancy

我想使用 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 如果图像大小 < 900kb,图像保存良好,但有时也会出现线条。 请帮忙。

最佳答案

据我所知,你有一个文件流,你想将它保存为图像

这里是一个和平的代码,在 Windows 8 .net 4.5 和 Ubuntu 12 TLS 中使用 mono JIT 编译器版本 2.10.8.1 运行良好

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 的字节数组

关于linux - 单声道流问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24369517/

相关文章:

html - 父 div,未设置高度,高度超出几个像素以适应子 img 元素高度?为什么?

java - 使用 Java 8 流查找 List<Object> 中值的总计

javascript - RxJS:解开嵌套的可观察量

python - 如何在 Python 中使用 cd 终端命令

Linux:如何设置一个应用程序使用不同的声卡?

linux - NSIS 支持 Linux 和 Solaris

html - 点击图片链接

html - 如何在标题图像顶部制作带有文本和 Logo 的标题图像?

linux - CMake:使用 MinGW 交叉编译 linux-to-windows 没有找到一些系统头文件

list - 类似映射的函数,在循环中的每次迭代中返回多个值