Search code examples
c#compressionscreenshotjpeg

How can I compress screenshot to send it via net?


My app make screenshot using methods from WinAPI. Screen is ok, it is saved as gif and has 76 kB. Jpg, png and other formats have more size. I have to send this file via net and it takes several seconds, about 2-3 seconds. It is posibility to compress this file in .NET or any free external software ? Quality don't have good, because I have to read only several labels.

Thanks


Solution

  • You could look at resizing the image in c#. It's fairly easy to do... here's some code I've used in the past to do this:

    Image image = Image.FromFile(fi.FullName);
    image = resizeImage(image, new Size(120, 120));
    EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)85);
    ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
    EncoderParameters encoderParams = new EncoderParameters(1);
    encoderParams.Param[0] = qualityParam;
    image.Save(OutSourceLoc + "/" + fi.Name, jpegCodec, encoderParams);
    
    
    static Image resizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;
    
            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;
    
            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);
    
            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;
    
            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);
    
            Bitmap b = new Bitmap(destWidth, destHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            b.SetResolution(300, 300);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighSpeed;
            g.SmoothingMode = SmoothingMode.HighSpeed;
    
            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();
    
            return (Image)b;
    
        }
    
        private static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            // Get image codecs for all image formats 
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
    
            // Find the correct image codec 
            for (int i = 0; i < codecs.Length; i++)
                if (codecs[i].MimeType == mimeType)
                    return codecs[i];
            return null;
        }