将图像按像素显示到面板中 [英] Displaying a image into panel by pixels

查看:72
本文介绍了将图像按像素显示到面板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一个由A之类的字母组成的位图图像.在面板中,我必须显示A(与我的位图图像中的字符相同),即按像素进行匹配.我使用代码

Hello,

I have a Bitmap image which consists of alphabet like A. In my Panel I have to display A(That is same as the character in my Bitmap image) that is by matching pixel wise.I am doing this with the code

public DrawingPanel()
        {
            InitializeDrawingPanel();
        }
        private void InitializeDrawingPanel()
        {
            CreateNewImage();
            base.Paint += new PaintEventHandler(DrawingPanel_Paint);
            base.Resize += new EventHandler(DrawingPanel_Resize);
        }
        void DrawingPanel_Resize(object sender, EventArgs e)
        {
            CreateNewImage();
        }
        void DrawingPanel_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.DrawImage(BufferImage, 0, 0);
            g.Dispose();
        }
       private void CreateNewImage()
        {
            BufferImage = new Bitmap(base.Width, base.Height);
            for (int i = 0; i < BufferImage.Height; i++)
                for (int j = 0; j < BufferImage.Width; j++)
                    BufferImage.SetPixel(j, i, Color.White);
        }




它的工作,但我不明白DrawingPanel_Paint,DrawingPanel_Resize以及他们为什么使用它.请帮助理解此代码.




its working but i dint understood DrawingPanel_Paint ,DrawingPanel_Resize and all why they are using it.Please help to understand this code.

推荐答案


就像没有DrawingPanel_Paint一样,您将只将图像存储在成员变量中,并且该图像将不可见.每当需要重绘窗口时,都会调用该事件,并在面板上绘制图像.
DrawingPanel_Resize事件的原因是,每当用户调整面板大小时,都会以与面板相同的大小来创建新图像.例如,如果您的面板尺寸为300x400,并将其尺寸调整为更大,则会生成新图像以覆盖所有面板区域.
尽管我不喜欢该代码,但这不是将图像的每个像素设置为白色的正确方法.这是不准确的,如果我正确记住了方法名称,则必须在位图上创建Graphics对象,然后调用"FillRectangle".
希望对您有所帮助.
问候,
Kemo
Hi,
as without the DrawingPanel_Paint you would have the image just stored in member variable, and it would not be visible. The event is called whenever your window need to be redrawn, and it draws the image on the panel.
The reason for the DrawingPanel_Resize event, is that whenever the user resizes the panel, the new image is created with the same size of the panel. For example, if your panel is 300x400 size, and resize it to be larger, the new image is generated to cover all panel area.
Although, I do not like the code, this is not the correct way to set each pixel of image with the white color. This is not accurate, you have to create Graphics object on the bitmap instead, and call "FillRectangle" if I remember the method name correctly.
I hope this helped.
Regards,
Kemo


这篇关于将图像按像素显示到面板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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