1

Does anyone have a good example of converting an image file coming from a HttpPostedFileBase to a reduced size and then converting the image to base64? I've spent hours on this with out any luck. Here is the start of my code. Some of which is hardcoded (image size).

This is giving me a black image when I place the base64 in an image tag and view it in a browser.

    public ActionResult Upload(HttpPostedFileBase file, decimal? id, decimal? id2)
    {                        
            Image img = Image.FromStream(file.InputStream, true, true);

            var bitmap = new Bitmap(img.Width - 100, img.Height - 100);

            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] imageBytes = stream.ToArray();
            string base64String = Convert.ToBase64String(imageBytes);
            InsertImage(base64String);
     }

I'm asking how to change the image then convert it to base64. This is more specific than the question called it duplicate.

J Hunt
  • 733
  • 1
  • 10
  • 21
  • Image has a GetThumbnailImage method that you can use to get a smaller image. Then you can use Save to write the new image to a stream. – Lorek Sep 26 '15 at 01:16
  • This is not a duplicate question – J Hunt Sep 26 '15 at 01:38
  • @JHunt reopened - I'm not sure what you need about base64 part - some clarification may be necessary. I hope whoever answers the question will explain code provided in the post too. – Alexei Levenkov Sep 26 '15 at 01:47

1 Answers1

6

I have never used the HttpPostedFileBase myself. So, I simplified the problem a bit, which is actually what you should try to do with future questions. You should try to narrow the focus as much as possible. That said, here is a method that reduces the dimensions of an image represented by a stream and returns the new image as an array of bytes.

    private static byte[] ReduceSize(FileStream stream, int maxWidth, int maxHeight)
    {
        Image source = Image.FromStream(stream);
        double widthRatio = ((double)maxWidth) / source.Width;
        double heightRatio = ((double)maxHeight) / source.Height;
        double ratio = (widthRatio < heightRatio) ? widthRatio : heightRatio;
        Image thumbnail = source.GetThumbnailImage((int)(source.Width * ratio), (int)(source.Height * ratio), AbortCallback, IntPtr.Zero);
        using (var memory = new MemoryStream())
        {
            thumbnail.Save(memory, source.RawFormat);
            return memory.ToArray();
        }
    }

You can probably call this method like this:

public ActionResult Upload(HttpPostedFileBase file, decimal? id, decimal? id2)
{
    byte[] imageBytes = ReduceSize(file.InputStream, 100, 100);
    string base64String = Convert.ToBase64String(imageBytes);
    InsertImage(base64String);
}

My ReduceSize() method maintains aspect ratio. You may not need that, and you may also want to change the arguments so you can change how you specify how it is resized. Give that a shot and let me know how it does.

Lorek
  • 855
  • 5
  • 11
  • It works great! Do you have any resources in how to learn more about image manipulation? How do I become more efficient in this topic? – J Hunt Sep 28 '15 at 18:00
  • 1
    Image manipulation is a huge topic. You'll probably just want to search online for specific tasks you want to accomplish. – Lorek Sep 28 '15 at 19:09