Getting an Image object from a byte array

前端 未结 4 1247
Happy的楠姐
Happy的楠姐 2021-02-07 08:37

I\'ve got a byte array for an image (stored in the database). I want to create an Image object, create several Images of different sizes and store them back in the database (sa

4条回答
  •  故里飘歌
    2021-02-07 08:53

    Based on your comments to another answer, you can try this for performing a transformation on an image that's stored in a byte[] then returning the result as another byte[].

    public byte[] TransformImage(byte[] imageData)
    {
        using(var input = new MemoryStream(imageData))
        {
            using(Image img = Image.FromStream(input))
            {
                // perform your transformations
    
                using(var output = new MemoryStream())
                {
                    img.Save(output, ImageFormat.Bmp);
    
                    return output.ToArray();
                }
            }
        }
    }
    

    This will allow you to pass in the byte[] stored in the database, perform whatever transformations you need to, then return a new byte[] that can be stored back in the database.

提交回复
热议问题