2

in my app I'm uploading images to server. I encode image from my WP 8.1 and send it to server. Sending and receiving work well but I have problem with image. I don't know why but whem I uploaded the image photographed on portrait after decoding is this image rotated by 90 degrees. Landscapes images are good, but portrait are rotated. Here is my encode and decode code

Encode:

 private async Task<string> StorageFileToBase64(StorageFile file)
        {
            string Base64String = "";

            if (file != null)
            {
                IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read);
                var reader = new DataReader(fileStream.GetInputStreamAt(0));
                await reader.LoadAsync((uint)fileStream.Size);
                byte[] byteArray = new byte[fileStream.Size];
                reader.ReadBytes(byteArray);
                Base64String = Convert.ToBase64String(byteArray);
            }

            return Base64String;
        }

Decode image on server

public void ShowImg(string b64)
            {
                byte[] imageBytes = Convert.FromBase64String(b64);
                // Convert byte[] to Image
                var ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

                Image image = Image.FromStream(ms, true);


                f.pictureBox1.Image = image;

            }
Matey
  • 197
  • 3
  • 5
  • 16
  • `Landscapes images are good, but portrait are rotated.` If you are not doing anything special for it, then why do you think your code is the problem. Maybe it is how the server handles your image.... – Eser Feb 28 '16 at 00:12
  • You can rotate when decode image ? [How do I rotate a picture in C#](http://stackoverflow.com/a/13160437/4869467) – Murat Gündeş Feb 28 '16 at 00:52
  • Yeah, but i don"t know if uploaded image is in landscape or portrait format – Matey Feb 28 '16 at 11:42
  • Did you ever find a solution to this? I am seeing the same issue. Thanks – scottsanpedro Mar 03 '17 at 16:15

1 Answers1

0
string startExpression = "data:image/png;base64,";
using (var ms = new MemoryStream())
{
   ms.Flush();       
   System.Drawing.Image imageIn = System.Drawing.Image.FromFile(fileName);  
   ms.Position = 0;                              
   imageIn.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipX);
   imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
   var bytes= ms.ToArray();
   string fileBase = Convert.ToBase64String(bytes); 
   var base64= startExpression + fileBase;
   imageIn.Dispose();
   ms.Dispose();
} 
  • 2
    Hope It will solve issue but please add explanation of your code with it so user will get perfect understanding which he/she really wants. – Jaimil Patel Jun 01 '20 at 06:02