C# 生成缩略图文件大小

发布于 2024-08-04 05:02:33 字数 163 浏览 3 评论 0原文

我有以下代码来拍摄图像并生成缩略图。

如何以编程方式改变质量或压缩以获得更小的文件大小?

Image thumbNail = image.GetThumbnailImage(Width, Height, null, new IntPtr());

I have the following code to take an image and generate the thumbnail.

how can I alter the quality or compression to get smaller file sizes programatically?

Image thumbNail = image.GetThumbnailImage(Width, Height, null, new IntPtr());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

诗化ㄋ丶相逢 2024-08-11 05:02:33

如果您确实需要更好地控制生成的缩略图,那么您最好通过手动生成较小尺寸和不同质量的图像来生成自己的缩略图。 GetThumbnailImage 不能给你太多的控制权。

请参阅这篇文章了解它是如何完成的。
http://www.switchonthecode.com/教程/csharp-tutorial-image-editing- saving-cropping-and-resizing

If you truly need better control over the thumbnails produced, you will be better off producing your own by manually generating an image of smaller size and different quality. The GetThumbnailImage dows not give you much control.

See this article for how it's done.
http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing

茶花眉 2024-08-11 05:02:33

使用 Image.Save 保存 thumbNail 您可以通过传递 EncoderParameter。请参阅:使用 C# 降低 JPEG 图片质量

EncoderParameter epQuality = new EncoderParameter(
    System.Drawing.Imaging.Encoder.Quality,
    (int)numQual.Value);

...
newImage.Save(..., iciJpegCodec, epParameters);

When saving the thumbNail with Image.Save you can specify the quality by passing a EncoderParameter. See: Reducing JPEG Picture Quality using C#

EncoderParameter epQuality = new EncoderParameter(
    System.Drawing.Imaging.Encoder.Quality,
    (int)numQual.Value);

...
newImage.Save(..., iciJpegCodec, epParameters);
思慕 2024-08-11 05:02:33

您不使用 GetThumbnailImage API:

protected Stream ResizeImage(string source, int width, int height) {
            using (System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(source))
            using (System.Drawing.Bitmap newBmp = new System.Drawing.Bitmap(width, height)) 
            using (System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(newBmp))
            {

                graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                graphic.DrawImage(bmp, 0, 0, width, height);

                MemoryStream ms = new MemoryStream();
                newBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                return ms;

            }             
        }

You do not use the GetThumbnailImage API:

protected Stream ResizeImage(string source, int width, int height) {
            using (System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(source))
            using (System.Drawing.Bitmap newBmp = new System.Drawing.Bitmap(width, height)) 
            using (System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(newBmp))
            {

                graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                graphic.DrawImage(bmp, 0, 0, width, height);

                MemoryStream ms = new MemoryStream();
                newBmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                return ms;

            }             
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文