简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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