0

I want to display an Image on a C# winForms application, but only with the quality of 50%, is it possible with the standard Picturebox?

Maybe resize the image programmatically to 50% of the size and then stretch it to the Picturebox?

Image img = Image.FromFile(imgFile)

** Resize the Image here, or do something else **

pictureBox.Image = img;
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
Filburt
  • 17,626
  • 12
  • 64
  • 115
  • Why do you need to resize it? – atoMerz Jan 18 '16 at 10:01
  • 4
    What exactly do you mean by `50% quality`? – Jens Jan 18 '16 at 10:02
  • @Jens, in some converters you can choose the JPEG Quality, this is what i want – Jannis Höschele Jan 18 '16 at 10:03
  • Ok, that is fundamentally different than stretching the image, so much I can say in a comment. – Jens Jan 18 '16 at 10:04
  • 50% quality means you want to decrease size of image or what ? – Darshan Faldu Jan 18 '16 at 10:05
  • 2
    "Quality" is a parameter for a codec, the software that creates an image file. The JPEG codec in particular uses it, the lower the quality, the smaller a file it can create. It has no meaning for PictureBox, you always see the pixels as-is. It's not like you can't make it look worse, make it smaller or bigger than the image size. Use the Bitmap(Image, int, int) constructor to create such a worse image. Pass a small width/height to get a small image that looks pretty lousy when you stretch it again. – Hans Passant Jan 18 '16 at 10:06
  • @DarshanPatel yes, i want to lower the quality – Jannis Höschele Jan 18 '16 at 10:06

2 Answers2

3

You can change the quality of Picture by following function,

private void VaryQualityLevel()
{
    // Get a bitmap.
    Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");
    ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

    // Create an Encoder object based on the GUID
    // for the Quality parameter category.
    System.Drawing.Imaging.Encoder myEncoder =
        System.Drawing.Imaging.Encoder.Quality;

    // Create an EncoderParameters object.
    // An EncoderParameters object has an array of EncoderParameter
    // objects. In this case, there is only one
    // EncoderParameter object in the array.
    EncoderParameters myEncoderParameters = new EncoderParameters(1);

    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 
        50L);
    myEncoderParameters.Param[0] = myEncoderParameter;
    bmp1.Save(@"c:\TestPhotoQualityFifty.jpg", jgpEncoder, 
        myEncoderParameters);

    myEncoderParameter = new EncoderParameter(myEncoder, 100L);
    myEncoderParameters.Param[0] = myEncoderParameter;
    bmp1.Save(@"c:\TestPhotoQualityHundred.jpg", jgpEncoder, 
        myEncoderParameters);

    // Save the bitmap as a JPG file with zero quality level compression.
    myEncoderParameter = new EncoderParameter(myEncoder, 0L);
    myEncoderParameters.Param[0] = myEncoderParameter;
    bmp1.Save(@"c:\TestPhotoQualityZero.jpg", jgpEncoder, 
        myEncoderParameters);

}

after assign 50% Quality Image on picture box,

pictureBox.Image = "c:\TestPhotoQualityFifty.jpg";

verify this stackoverflow.com/questions/1484759/quality-of-a-saved-jpg-in-c-sharp

Community
  • 1
  • 1
0

I suggest you to first read this -
How to: Set JPEG Compression Level
Turning off JPEG color downsampling for Bitmap.Save()

You can save it to MemoryStream and then load a new bitmap from this MemoryStream.

Try this approach:

// Get a bitmap.
Bitmap bmp1 = new Bitmap(@"c:\TestPhoto.jpg");
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);

// Create an Encoder object based on the GUID
// for the Quality parameter category.
System.Drawing.Imaging.Encoder myEncoder =
    System.Drawing.Imaging.Encoder.Quality;

// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
EncoderParameters myEncoderParameters = new EncoderParameters(1);

EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
myEncoderParameters.Param[0] = myEncoderParameter;
MemoryStream ms = new MemoryStream();
bmp1.Save(ms, jpgEncoder, myEncoderParameters); 
pictureBox.Image = Image.FromStream(ms);

.....

private ImageCodecInfo GetEncoder(ImageFormat format)
{

    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

    foreach (ImageCodecInfo codec in codecs)
    {
        if (codec.FormatID == format.Guid)
        {
            return codec;
        }
    }
    return null;
}
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75