如何压缩截图以通过网络发送?

时间:2012-03-05 21:57:04

标签: c# compression screenshot jpeg

我的应用使用WinAPI中的方法制作屏幕截图。屏幕没问题,保存为gif,有76 kB。 Jpg,png和其他格式有更大的尺寸。我必须通过网络发送此文件,它需要几秒钟,大约2-3秒。可以在.NET或任何免费的外部软件中压缩这个文件吗?质量没有好处,因为我只需阅读几个标签。

由于

3 个答案:

答案 0 :(得分:2)

您可以在c#中查看调整图像大小。这很容易做到......这里是我过去用过的一些代码:

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;
    }

答案 1 :(得分:1)

如果你需要的只是文字,也许你可以减少颜色,例如4色灰色图像?

答案 2 :(得分:0)

除非您尝试将多个图像合并到一个文件中(例如,将它们合并到一个Zip文件中),否则我看不到压缩图像的重点。

JPG,GIF或PNG几乎已经被压缩了。例如,使用GZIP压缩现有文件会浪费计算资源而不会受益。

JPG,GIF,PNG就像mp3一样使用压缩。

来自维基百科:

  

便携式网络图形(PNG /pɪŋ/ [2])是一种位图图像格式,采用无损数据压缩

     

JPEG(/dʒeɪpɛɡ/ [发音为jay-peg]是数码摄影(图像)有损压缩的常用方法

     

使用Lempel-Ziv-Welch(LZW)无损数据压缩技术压缩GIF图像,以减小文件大小而不降低视觉质量

相关问题