C#将图像转换为完全的黑白

3
我有一张gif图片,但它太大了,即使只有100~300kb也不行。
在Photoshop中,我通过减少颜色数量到2(黑白)的方式将160kb gif压缩到15kb(仅为原大小的1/10)。
我想在我的应用程序中做同样的事情,但我只能找到将图像转换为灰度的方法,这将我的160kb gif转换为100kb。
有没有办法将我的gif完全转换为黑白?如果有其他方法可以将gif缩小到更小的尺寸,那就更好了。
6个回答

4
这是一个关于如何将图片转化为双色G4压缩的TIFF格式的示例代码。需要注意的是,这种方法适用于有大量空白和文本的图片,但对于其他类型的图片效果不佳。如果处理其他类型的图片,你可能需要查看其他答案并使用抖动技术。

我有一张图片,它把我的160kb压缩到了9kb。易于理解。完美。 - Or Betzalel

1

C#中将图像转换为黑白

/*
Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

#region Usings
using System.Drawing;
using System.Drawing.Imaging;
#endregion

 namespace Utilities.Media.Image
 {
    /// <summary>
    /// Helper class for setting up and applying a color matrix
     /// </summary>
     public class ColorMatrix
    {
         #region Constructor

        /// <summary>
        /// Constructor
         /// </summary>
         public ColorMatrix()
         {
       }

         #endregion

           #region Properties

         /// <summary>
         /// Matrix containing the values of the ColorMatrix
         /// </summary>
         public float[][] Matrix { get; set; }

         #endregion

         #region Public Functions

         /// <summary>
         /// Applies the color matrix
         /// </summary>
         /// <param name="OriginalImage">Image sent in</param>
         /// <returns>An image with the color matrix applied</returns>
        public Bitmap Apply(Bitmap OriginalImage)
         {
             using (Graphics NewGraphics = Graphics.FromImage(NewBitmap))
             {
                 System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(Matrix);
                 using (ImageAttributes Attributes = new ImageAttributes())
                {
                     Attributes.SetColorMatrix(NewColorMatrix);
                     NewGraphics.DrawImage(OriginalImage,
                         new System.Drawing.Rectangle(0, 0, OriginalImage.Width, OriginalImage.Height),
                         0, 0, OriginalImage.Width, OriginalImage.Height,
                         GraphicsUnit.Pixel,
                         Attributes);
                 }
             }
             return NewBitmap;
         }

         #endregion
     }
 }


 /// <summary>
/// Converts an image to black and white
/// </summary>
/// <param name="Image">Image to change</param>
/// <returns>A bitmap object of the black and white image</returns>
public static Bitmap ConvertBlackAndWhite(Bitmap Image)
{
     ColorMatrix TempMatrix = new ColorMatrix();
    TempMatrix.Matrix = new float[][]{
                     new float[] {.3f, .3f, .3f, 0, 0},
                    new float[] {.59f, .59f, .59f, 0, 0},
                     new float[] {.11f, .11f, .11f, 0, 0},
                    new float[] {0, 0, 0, 1, 0},
                    new float[] {0, 0, 0, 0, 1}
                };
     return TempMatrix.Apply(Image);
}

 float[][] FloatColorMatrix ={ 
         new float[] {1, 0, 0, 0, 0}, 
         new float[] {0, 1, 0, 0, 0}, 
        new float[] {0, 0, 1, 0, 0}, 
        new float[] {0, 0, 0, 1, 0}, 
         new float[] {0, 0, 0, 0, 1} 
     };

1
这里有一些关于编程的内容在SO上:拜尔有序抖动,据说可以实现你想要的功能(未经测试)。值得一试。

1

您可以使用ImageMagick来实现这个功能。可以通过命令行运行Process.Start,或者使用作为Windows安装的一部分的COM接口。选项“-monochrome”是您的好帮手。


0

-1

Chuck Conway发布的解决方案的工作版本

 /*
Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

using System.Drawing;
using System.Drawing.Imaging;

namespace WebCamService {
    class ColorMatrix {

        public float[][] Matrix { get; set; }

        public Bitmap Apply(Bitmap OriginalImage) {
            using (Graphics NewGraphics = Graphics.FromImage(OriginalImage)) {
                System.Drawing.Imaging.ColorMatrix NewColorMatrix = new System.Drawing.Imaging.ColorMatrix(Matrix);
                using (ImageAttributes Attributes = new ImageAttributes()) {
                    Attributes.SetColorMatrix(NewColorMatrix);
                    NewGraphics.DrawImage(OriginalImage,
                        new System.Drawing.Rectangle(0, 0, OriginalImage.Width, OriginalImage.Height),
                        0, 0, OriginalImage.Width, OriginalImage.Height,
                        GraphicsUnit.Pixel,
                        Attributes);
                }
            }
            return OriginalImage;
        }

        public static Bitmap ConvertBlackAndWhite(Bitmap Image) {
            ColorMatrix TempMatrix = new ColorMatrix();
            TempMatrix.Matrix = new float[][]{
                     new float[] {.3f, .3f, .3f, 0, 0},
                    new float[] {.59f, .59f, .59f, 0, 0},
                     new float[] {.11f, .11f, .11f, 0, 0},
                    new float[] {0, 0, 0, 1, 0},
                    new float[] {0, 0, 0, 0, 1}
                };
            return TempMatrix.Apply(Image);
        }


    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接