将位图转换为灰度 [英] Converting Bitmap to grayscale

查看:94
本文介绍了将位图转换为灰度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图像从位图转换为灰度,但是当我将其加载到图片框中时...不在按钮单击事件上...
这样...我加载一个位图图片并在图片框中,它直接显示为灰度图片...
我在Internet上找到了一些源代码,但是我并没有真正知道如何将代码放在哪里做我想做的事...
谢谢

I want to convert an image from bitmap to grayscale, but when I load it in a picturebox...not on a button click event...
Like this...I load a bitmap picture and in the picturebox and it appears directly as a grayscale picture...
I found some source code on the Internet, but I don''t really kmow how where to put the code to do what I want...
Thank you

推荐答案

啊,谷歌,你为什么抛弃了我以外的所有人? Google短语"c#将彩色图像转换为黑白图像",这是202,000个返回结果之一:

Ahh, google, why hath thou forsaken everyone but me? Google the phrase "c# convert color image to black and white", and this is one of the 202,000 returned results:

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


您会发现本教程很有帮助,因为它显示了三种不同的方式来完成您想要的事情.最后一个是我的最爱,它通过使用ColorMatrix来达到目的: http: //www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale [
You''ll find this tutorial helpful as it shows three different ways to do what you want. The last is my favorite and does its trick by using a ColorMatrix: http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale[^]!

Hope this helps you!

Modification
Code sample: All you need to create is a form with a PictureBox on it!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace GrayImage
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
       
            //Load an image frim you local file system
            Image normalImage = Image.FromFile(@"D:\Data\Media\Photo\2009\25-12-2009\DSC_0073.jpg");
            // Turn image into gray scale image
            Image grayScaled = (Image)MakeGrayscale3(new Bitmap(normalImage));
            
            pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
            // Assign image to picture box
            pictureBox1.BackgroundImage = grayScaled;
        }

        // This is the method form the Web site I told you about
        public static Bitmap MakeGrayscale3(Bitmap original)
        {
            //create a blank bitmap the same size as original
            Bitmap newBitmap = new Bitmap(original.Width, original.Height);
            //get a graphics object from the new image
            Graphics g = Graphics.FromImage(newBitmap);
            //create the grayscale ColorMatrix
            ColorMatrix colorMatrix = new ColorMatrix(
               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}
              });
            //create some image attributes
            ImageAttributes attributes = new ImageAttributes();
            //set the color matrix attribute
            attributes.SetColorMatrix(colorMatrix);
            //draw the original image on the new image
            //using the grayscale color matrix
            g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
               0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
            //dispose the Graphics object
            g.Dispose();
            return newBitmap;
        }
    }
}



最好的问候,
曼弗雷德(Manfred)



Best Regards,
Manfred


这篇关于将位图转换为灰度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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