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

问题描述

我想将图像从位图转换为灰度,但是当我将其加载到图片框中时...不在按钮单击事件上...
这样...我加载一个位图图片并在图片框中,它直接显示为灰度图片...
我在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

推荐答案

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


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


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

07-23 11:13