简体   繁体   English

MemoryStream 进入 MagicImage

[英]MemoryStream into MagicImage

I am trying to store MemoryStream into MagicImage, but when I am trying to upload file with heic format it still uploading with heic format, but it should upload it with jpeg format.我正在尝试将 MemoryStream 存储到 MagicImage 中,但是当我尝试以 heic 格式上传文件时,它仍然以 heic 格式上传,但它应该以 jpeg 格式上传。 So I kind of do not understand where I am doing wrong.所以我有点不明白我在哪里做错了。 So could someone help me?那么有人可以帮助我吗?

                using (MemoryStream ms = new MemoryStream())
                {
                    create.PostedFile.InputStream.CopyTo(ms);
                    var data = ms.ToArray();


                    byte[] data1 = null;
                    using (var image = new MagickImage(data))
                    {
                        // Sets the output format to jpeg
                        image.Format = MagickFormat.Jpeg;

                        // Create byte array that contains a jpeg file
                        data1 = image.ToByteArray();
                    }

                    var file = new ClientFile
                    {
                        Data = data1, // here where it should store it in jpeg
                    };

While I have never used your way of writing the image, this is what I use in my implementations and it always works:虽然我从未使用过您编写图像的方式,但这是我在实现中使用的方式,并且它始终有效:

var image = new MagickImage(sourceStream);
var format = MagickFormat.Jpg;
var stream = new MemoryStream();
image.Write(stream, format);
stream.Position = 0;

EDIT If you don't add:编辑如果您不添加:

stream.Position = 0

sending the stream will not work as it will start saving from the current position which is at the end of the stream.发送 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ 将不起作用,因为它将从当前 position 开始保存,该 position 位于 stream 的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM