Пример #1
0
        public static byte[] CreateThumbnail(byte[] imageByte, bool maintainAspectRatio, int desiredWidth, int desiredHeight)
        {
            byte[] byteArray = new byte[0];
            Bitmap bmp;
            try
            {
                MemoryStream memStream = new MemoryStream(imageByte);
                System.Drawing.Image img = System.Drawing.Image.FromStream(memStream);

                if (maintainAspectRatio)
                {
                    AspectRatio aspectRatio = new AspectRatio();
                    aspectRatio.WidthAndHeight(img.Width, img.Height, desiredWidth, desiredHeight);
                    bmp = new Bitmap(img, aspectRatio.Width, aspectRatio.Height);
                }
                else
                {
                    bmp = new Bitmap(img, desiredWidth, desiredHeight);
                }
                byteArray = ToByteArray(bmp, ImageFormat.Jpeg);
                memStream.Dispose();
            }
            catch (Exception ex)
            {

            }
            return byteArray;
        }
Пример #2
0
        public static byte[] ReduceSize(byte[] imageByte)
        {
            byte[] byteArray = new byte[0];
            Bitmap bmp;

            try
            {
                using (MemoryStream memStream = new MemoryStream(imageByte))
                {
                    System.Drawing.Image img = System.Drawing.Image.FromStream(memStream);

                    if (img.Width != 1024 || img.Height != 384)
                    {
                        AspectRatio aspectRatio = new AspectRatio();
                        aspectRatio.WidthAndHeight(img.Width, img.Height, 1024, 384);
                        bmp = new Bitmap(img, aspectRatio.Width, aspectRatio.Height);

                        byteArray = ToByteArray(bmp, ImageFormat.Jpeg);

                    }
                    else
                    {
                        byteArray = imageByte;
                    }
                }
            }
            catch (Exception ex)
            {

            }

            return byteArray;
        }