Ruslan Abilkaiyrov
Ruslan Abilkaiyrov

Reputation: 19

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? I am trying it open in Frame in web, but it does not open bc it is not converting it to jpeg.

                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
                    };

Upvotes: 0

Views: 888

Answers (1)

JTO
JTO

Reputation: 156

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.

Upvotes: 2

Related Questions