0

Am working in windows form.I have an image. Its size is 960*1280. enter image description here

When i tried to add this image to my picture box at run time. the image is getting rotated and its size of the image is 1280*960.

enter image description here

my aim is to resize the image to 100*100 and then add to picture box. i don't want that image to get rotated. Can you suggest me some ideas?

user2115618
  • 183
  • 1
  • 1
  • 13

2 Answers2

0

put this in a class file and use the below resizer code

 public class ImageResizer
{
    private int allowedFileSizeInByte;
    private string sourcePath;
    private string destinationPath;

    public ImageResizer()
    {

    }

    public ImageResizer(int allowedSize, string sourcePath, string destinationPath)
    {
        allowedFileSizeInByte = allowedSize;
        this.sourcePath = sourcePath;
        this.destinationPath = destinationPath;
    }

    public void ScaleImage()
    {
        using (MemoryStream ms = new MemoryStream())
        {
            using (FileStream fs = new FileStream(sourcePath, FileMode.Open))
            {
                Bitmap bmp = (Bitmap)Image.FromStream(fs);
                SaveTemporary(bmp, ms, 100);

                while (ms.Length < 0.9 * allowedFileSizeInByte || ms.Length > allowedFileSizeInByte)
                {
                    double scale = Math.Sqrt((double)allowedFileSizeInByte / (double)ms.Length);
                    ms.SetLength(0);
                    bmp = ScaleImage(bmp, scale);
                    SaveTemporary(bmp, ms, 100);
                }

                if (bmp != null)
                    bmp.Dispose();
                SaveImageToFile(ms);
            }
        }
    }

    public byte[] GetScaledImage(int allowedSize, string sourcePath)
    {
        allowedFileSizeInByte = allowedSize;
        this.sourcePath = sourcePath;
        //this.destinationPath = destinationPath;

        using (MemoryStream ms = new MemoryStream())
        {
            using (FileStream fs = new FileStream(sourcePath, FileMode.Open))
            {
                Bitmap bmp = (Bitmap)Image.FromStream(fs);
                SaveTemporary(bmp, ms, 100);

                while (ms.Length < 0.9 * allowedFileSizeInByte || ms.Length > allowedFileSizeInByte)
                {
                    double scale = Math.Sqrt((double)allowedFileSizeInByte / (double)ms.Length);
                    ms.SetLength(0);
                    bmp = ScaleImage(bmp, scale);
                    SaveTemporary(bmp, ms, 100);
                }

                if (bmp != null)
                    bmp.Dispose();

                Byte[] buffer = null;

                if (ms != null && ms.Length > 0)
                {
                    ms.Position = 0;
                    buffer = new byte[ms.Length];
                    ms.Read(buffer, 0, buffer.Length);
                }

                return buffer;
            }
        }
    }

    private void SaveImageToFile(MemoryStream ms)
    {
        byte[] data = ms.ToArray();

        using (FileStream fs = new FileStream(destinationPath, FileMode.Create))
        {
            fs.Write(data, 0, data.Length);
        }
    }

    private void SaveTemporary(Bitmap bmp, MemoryStream ms, int quality)
    {
        EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
        var codec = GetImageCodecInfo();
        var encoderParams = new EncoderParameters(1);
        encoderParams.Param[0] = qualityParam;

        if (codec != null)
            bmp.Save(ms, codec, encoderParams);
        else
            bmp.Save(ms, GetImageFormat());
    }

    public Bitmap ScaleImage(Bitmap image, double scale)
    {
        int newWidth = (int)(image.Width * scale);
        int newHeight = (int)(image.Height * scale);

        Bitmap result = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
        result.SetResolution(image.HorizontalResolution, image.VerticalResolution);

        using (Graphics g = Graphics.FromImage(result))
        {
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.DrawImage(image, 0, 0, result.Width, result.Height);
        }
        return result;
    }

    private ImageCodecInfo GetImageCodecInfo()
    {
        FileInfo fi = new FileInfo(sourcePath);

        switch (fi.Extension)
        {
            case ".bmp": return ImageCodecInfo.GetImageEncoders()[0];
            case ".jpg":
            case ".jpeg": return ImageCodecInfo.GetImageEncoders()[1];
            case ".gif": return ImageCodecInfo.GetImageEncoders()[2];
            case ".tiff": return ImageCodecInfo.GetImageEncoders()[3];
            case ".png": return ImageCodecInfo.GetImageEncoders()[4];
            default: return null;
        }
    }

    private ImageFormat GetImageFormat()
    {
        FileInfo fi = new FileInfo(sourcePath);

        switch (fi.Extension)
        {
            case ".jpg": return ImageFormat.Jpeg;
            case ".bmp": return ImageFormat.Bmp;
            case ".gif": return ImageFormat.Gif;
            case ".png": return ImageFormat.Png;
            case ".tiff": return ImageFormat.Tiff;
            default: return ImageFormat.Png;
        }
    }
}

here is the code for resize the image

  byte[] compressedBuffer = new ImageResizer().GetScaledImage(300000, FileName);

here 30000 shows the size, and the filename is the name of the file

Thomas
  • 1,445
  • 14
  • 30
  • Note that this will take processing power. Where the original problem could be solved by proper use of the loaded image or from the ImageControl itself – Khalil Khalaf Sep 19 '16 at 12:35
  • My is windows form ImageResizer class could not found. – user2115618 Sep 19 '16 at 12:39
  • It's not working. In the method GetScaledImage you have that Bitmap bmp = new (Bitmap)Image.FromStream(fs); When i checked that bmp.Size in quick watch window the size is changed. the original size is not taking – user2115618 Sep 20 '16 at 05:39
0

One of the Bitmap class constructors takes an original image and a new size as an input:

Image uploadedImage = new Bitmap("Path/to/your/image.bmp");
Image resizedImage = new Bitmap(uploadedImage, new Size(100, 100));

In the first line of the sample you load the image. In the second line of the example you create a new object using the uploaded image and a new size.

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68