Click here to Skip to main content
16,010,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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



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

1 solution

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
 
Share this answer
 
Comments
swathi6589 14-Oct-10 2:14am    
Thank you for your answer,Please can you tell me the other method which you suggested at the end of your paragraph.
Kemo72000 14-Oct-10 6:57am    
public void CreateImage()
{
BufferImage = new Bitmap(base.Width, base.Height);
using (Graphics graphics = Graphics.FromImage(BufferImage))
{
graphics.FillRectangle(Brushes.White, 0, 0, BufferImage.Width, BufferImage.Height);
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900