Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

c# resize image from byte array

public static byte[] Resize2Max50Kbytes(byte[] byteImageIn)
{
    byte[] currentByteImageArray = byteImageIn;
    double scale = 1f;

    if (!IsValidImage(byteImageIn))
    {
        return null;
    }

    MemoryStream inputMemoryStream = new MemoryStream(byteImageIn);
    Image fullsizeImage = Image.FromStream(inputMemoryStream);

    while (currentByteImageArray.Length > 50000)
    {
        Bitmap fullSizeBitmap = new Bitmap(fullsizeImage, new Size((int)(fullsizeImage.Width * scale), (int)(fullsizeImage.Height * scale)));
        MemoryStream resultStream = new MemoryStream();

        fullSizeBitmap.Save(resultStream, fullsizeImage.RawFormat);

        currentByteImageArray = resultStream.ToArray();
        resultStream.Dispose();
        resultStream.Close();

        scale -= 0.05f;
    }

    return currentByteImageArray;
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #resize #image #byte #array
ADD COMMENT
Topic
Name
8+4 =