开发者

Custom control for displaying images

I have my own classes that represent image data. They have various underlying structure but all have indexed property which returns an i,j-th image element. I need to display such images in a form. Right now I pre-convert them to System.Drawing.Bitmap and then display it in a standard Winforms PictureBox. My idea is to create a custom control that will have a property of my image type and will display a picture without preliminary converting it to System.Drawing.Bitmap or System.Drawing.Image.

I suggest that the clue to this problem is creating a user-drawn control and overriding OnPaint event there.

protected override void  OnPaint(PaintEventArgs e)
    {
        for(int i=e.ClipRectangle.Left; i<=e.ClipRectangle.Right; i++)
        {
            for (int j = e.ClipRectangle.Top; j <= e.ClipRectangle.Bottom; j++)
       开发者_StackOverflow中文版     {
                //do something with i and j here
                //calculate the corresponding k and l indices
                //assign _myCustomClassImage[k,l] somewhere
            }
        }

    }

Yes, I understand that I can create Bitmap of the clip rectangle's size and use SetPixel to assign it's elements but SetPixel works very slowly.

Another idea is to use marshalling to assign bitmap's pixels but the whole idea of creating a bitmap inside an OnPaint event handler seems quite lame.

Are there any suggestions in what direction should I continue my work?


Creating a custom control and overriding the OnPaint method would be of value if the data you wanted to display were vectorial in some way. If you have an image to display, there does not seem to be a better alternative than creating a Bitmap object.

I know SetPixel is slow and I am not sure what you mean with 'use marshalling to assign bitmap's pixels' but there is a fast alternative using the System.Drawing.Imaging.BitmapData class and Bitmap.LockBits method. See an example usage here.

By the way, why do you need to create the Bitmap object in the OnPaint method? Is not creating and assigning it once enough?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜