如何降低图像的C#和.NET 3.5的大小? [英] How to reduce the size of an image in C# and .NET 3.5?

查看:186
本文介绍了如何降低图像的C#和.NET 3.5的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个屏幕截图,我对我的移动应用程序。下面的屏幕截图大约需要32 KB时,保存为一个磁盘上的PNG。

我送这些到中央SQL Server和32 KB过大的倍量我将需要存储的屏幕快照(约2500天次)。

有没有什么样的挂羊头卖狗肉的,我可以做的就是它救小?

下面是位图来个字节(发送到服务器存储)在code我现在使用把它:

  MemoryStream的流=新的MemoryStream();
 _signatureImage.Save(流ImageFormat.Png);
 返回stream.ToArray();
 

_signatureImage 位图,是有问题的截图。

下面的屏幕截图我现在的储蓄的例子:

东西,突然在脑海中(但我不知道该怎么做这些):

  1. 缩小图像的实际高度和宽度(但希望在不歪曲的方式)。
  2. 将其更改为黑白图像(不知道我是否会看到任何真正的节省了空间,从这个)
  3. 的COM preSS它更多的(我不喜欢这个尽可能多的,因为那是无法读取的数据库)。

请注意,这一切都必须通过程序来完成,而不能采取很长,很复杂的图像操作都出来了。

感谢您的帮助。

解决方案

 私有静态图像ResizeImage(INT newSize,图片originalImage)
    {
        如果(originalImage.Width< = newSize)
            newSize = originalImage.Width;

        VAR newHeight = originalImage.Height * newSize / originalImage.Width;

        如果(newHeight> newSize)
        {
            //随高度调整,而不是
            newSize = originalImage.Width * newSize / originalImage.Height;
            newHeight = newSize;
        }

        返回originalImage.GetThumbnailImage(newSize,newHeight,空,IntPtr.Zero);
    }
 

这应该与你的位图对象,工作类型和调整的高度或宽度,这取决于哪一个适合您的图片尺寸。它也将保持规模。

编辑:

您可以创建一个新的位图对象,并调整您的原始图像到该位图对象。

 位图B =新位图(newWidth,newHeight);
图形G = Graphics.FromImage((图)B);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.DrawImage(imgToResize,0,0,newWidth,newHeight);
g.Dispose();

返回(图)B:
 

我没有安装Compact Framework的,但似乎这应该为你工作。

I have a screen shot I take in my mobile app. The screen shot takes about 32 KB when saved as a png on a disk.

I am sending these to a central SQL Server and 32 KB is too big for that amount of times I will need to store that screen shot (approx 2500 times a day).

Is there any kind of trickery that I can do to get it to save smaller?

Here is the code I am using now to take it from Bitmap to bytes (to send to the server for storage):

MemoryStream stream = new MemoryStream();
 _signatureImage.Save(stream, ImageFormat.Png);
 return stream.ToArray();

_signatureImage is a Bitmap and is the screenshot in question.

Here is an example of the screen shot I am saving:

Things that pop to mind (but I don't know how to do them):

  1. Reduce the actual Height and Width of the image (but hopefully in a way that will not distort it).
  2. Change it to a black and white image (not sure if I will see any real space savings from this)
  3. Compress it more (I don't like this as much because then it is not readable from the database).

Note, this all has to be done programatically, and cannot take very long, so complex image manipulations are out.

Thanks for any help.

解决方案

    private static Image ResizeImage(int newSize, Image originalImage)
    {
        if (originalImage.Width <= newSize)
            newSize = originalImage.Width;

        var newHeight = originalImage.Height * newSize / originalImage.Width;

        if (newHeight > newSize)
        {
            // Resize with height instead
            newSize = originalImage.Width * newSize / originalImage.Height;
            newHeight = newSize;
        }

        return originalImage.GetThumbnailImage(newSize, newHeight, null, IntPtr.Zero);
    }

This should work with your Bitmap object Type and resize the Height or Width, depending on which is appropriate for your image dimensions. It will also maintain scale.

EDIT:

You could create a new Bitmap object and resize your original image into that Bitmap object.

Bitmap b = new Bitmap(newWidth, newHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.DrawImage(imgToResize, 0, 0, newWidth, newHeight);
g.Dispose();

return (Image)b;

I don't have the Compact Framework installed, but it seems that this should work for you.

这篇关于如何降低图像的C#和.NET 3.5的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆