Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#将图像转换为完整的黑色&;白色_C#_Image Processing_Gif_Screen Capture - Fatal编程技术网

C#将图像转换为完整的黑色&;白色

C#将图像转换为完整的黑色&;白色,c#,image-processing,gif,screen-capture,C#,Image Processing,Gif,Screen Capture,我有一个gif图像,它对于我的需要来说太大了(即使它是100~300kb) 在photoshop中,我只需将他使用的颜色减少到2种(黑白),就可以将160kb的gif变成15kb(他大小的1/10!!) 我想在我的应用程序中做同样的事情,但我能找到的只是将图像转换为灰度,这将我的160kb gif转换为100kb 有没有办法把我的gif变成黑白的?任何其他可以将gif缩小到更小尺寸的方法都将受到欢迎。查看Floyd Steinberg抖动: 或者,如果您不需要抖动,请找到平均亮度,然后将高于该亮

我有一个gif图像,它对于我的需要来说太大了(即使它是100~300kb)

在photoshop中,我只需将他使用的颜色减少到2种(黑白),就可以将160kb的gif变成15kb(他大小的1/10!!)

我想在我的应用程序中做同样的事情,但我能找到的只是将图像转换为灰度,这将我的160kb gif转换为100kb


有没有办法把我的gif变成黑白的?任何其他可以将gif缩小到更小尺寸的方法都将受到欢迎。

查看Floyd Steinberg抖动:


或者,如果您不需要抖动,请找到平均亮度,然后将高于该亮度的所有像素都变成白色,低于该亮度的所有像素变成黑色。

这里有一些代码,因此:我认为应该这样做(未测试)。值得一试。

以下是如何将其转换为双音G4压缩TIFF的示例。请注意,这对于包含大量空白和文本的图像非常好,但对于图像则不太好。对于图像,您可能希望看到其他答案并使用抖动。

/*
版权所有(c)2010
特此向任何获得副本的人免费授予许可
本软件和相关文档文件(“软件”)的
在软件中不受限制,包括但不限于权利
使用、复制、修改、合并、发布、分发、再许可和/或销售
软件的副本,并允许向其提供软件的人员
按照以下条件提供:
上述版权声明和本许可声明应包含在
软件的所有副本或主要部分。
本软件按“原样”提供,无任何形式的明示或明示担保
默示,包括但不限于适销性保证,
适用于特定目的和非侵权。在任何情况下
作者或版权持有人应承担任何索赔、损害或其他责任
无论是在合同诉讼、侵权诉讼或其他诉讼中,由以下原因引起的责任:,
与本软件有关或与本软件的使用或其他交易有关
软件*/
#区域使用
使用系统图;
使用系统、绘图、成像;
#端区
命名空间实用程序.Media.Image
{
/// 
///用于设置和应用颜色矩阵的帮助器类
/// 
公共类颜色矩阵
{
#区域构造函数
/// 
///建造师
/// 
公共颜色矩阵()
{
}
#端区
#区域属性
/// 
///包含颜色矩阵值的矩阵
/// 
公共浮点[]矩阵{get;set;}
#端区
#区域公共职能
/// 
///应用颜色矩阵
/// 
///图像已发送进来
///应用颜色矩阵的图像
公共位图应用(位图原始图像)
{
使用(Graphics NewGraphics=Graphics.FromImage(NewBitmap))
{
System.Drawing.Imaging.ColorMatrix NewColorMatrix=新系统.Drawing.Imaging.ColorMatrix(矩阵);
使用(ImageAttributes=new ImageAttributes())
{
属性。SetColorMatrix(NewColorMatrix);
NewGraphics.DrawImage(原始图像,
新系统。绘图。矩形(0,0,原始图像。宽度,原始图像。高度),
0,0,原始图像。宽度,原始图像。高度,
像素,
属性);
}
}
返回NewBitmap;
}
#端区
}
}
/// 
///将图像转换为黑白
/// 
///改变形象
///黑白图像的位图对象
公共静态位图转换器黑白(位图图像)
{
ColorMatrix TempMatrix=新ColorMatrix();
TempMatrix.Matrix=新浮点[][]{
新浮点[]{.3f、.3f、.3f、0、0},
新浮点[]{.59f、.59f、.59f、0、0},
新浮点[]{.11f、.11f、.11f、0、0},
新浮点[]{0,0,0,1,0},
新浮点[]{0,0,0,0,1}
};
返回TempMatrix.Apply(图像);
}
float[][]FloatColorMatrix={
新浮点[]{1,0,0,0,0},
新浮点[]{0,1,0,0,0},
新浮点[]{0,0,1,0,0},
新浮点[]{0,0,0,1,0},
新浮点[]{0,0,0,0,1}
};

您可以使用它。通过
Process.Start通过命令行运行它,或者使用COM界面(Windows安装的一部分)。选项“-单色”是你的朋友。

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


    }
}
/*
版权所有(c)2010
特此向任何获得副本的人免费授予许可
本软件和相关文档文件(“软件”)的
在软件中不受限制,包括但不限于权利
使用、复制、修改、合并、发布、分发、再许可和/或销售
软件的副本,并允许向其提供软件的人员
按照以下条件提供:
上述版权声明和本许可声明应包含在
软件的所有副本或主要部分。
本软件按“原样”提供,无任何形式的明示或明示担保
默示,包括但不限于适销性保证,
适用于特定目的和非侵权。在任何情况下
作者或版权持有人应承担任何索赔、损害或其他责任
无论是在合同诉讼、侵权诉讼或其他诉讼中,由以下原因引起的责任:,
与本软件有关或与本软件的使用或其他交易有关
软件*/
使用系统图;
使用系统、绘图、成像;
命名空间网络摄像机服务{
 /*
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);
        }


    }
}