Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I get a screenshot of the PropertyEditor control? DrawToBitmap not working

Solved!
Go to solution

Nevermind, I just discovered your post that has your example attached here. I'll work with this example. Is this the most up to date example you have?

 

 

I see you have been very busy with several other posts, I have linked the relevant posts here for reference:

 

Host a PropertyEditor in a DataGridView cell
PropertyEditor DrawToImage fails in windows xp but not in windows 7
Add PropertyEditor as custom datagridviewcolumn in datagridview
Custom datagridview cell is turning blank on cell leave
PropertyEditor, object property gets null (None)
PropertyEditor as custom datagridviewcolumn - cell not visible

National Instruments
0 Kudos
Message 21 of 25
(3,105 Views)

Just wanted to update you on my progress with this. I was able to reproduce what you are seeing and only on XP. It appears to be an issue with DrawToBitmap as you described. It is not clear whether this is an issue with .NET or with our implementation. I'm still investigating, but will keep you updated with what I find.

National Instruments
0 Kudos
Message 22 of 25
(3,102 Views)
Solution
Accepted by tonitpp

It looks like this is not an issue with our control, instead it is an issue with the TextBox control. You can see that if you create a new TextBox control, DrawToBitmap doesn't work on a TextBox control by itself.

 

You had mentioned the response you got from Microsoft that lists the scenarios that DrawToBitmap does not work in here. But none of these scenarios apply. Item #2 does not apply because this is not Tablet edition, and I don't think it is an Ink control anyway. Also, the visible property was already true.

 

I have not been able to find any sufficient answer from Microsoft on this issue, but I did find a workaround in this blog. He suggests setting the Multiline property of the TextBox to True and that resolves the issue. In fact this did work when I applied that to the TextBox child control of the PropertyEditor control. I used the following code to achieve this.

 

((TextBox)propertyEditor.Controls[1]).Multiline = true;

 

National Instruments
Message 23 of 25
(3,100 Views)

Yes, You are rigth, it was happening only when using DrawToBitmap function and only in Windows XP. So setting multiline to true as you said, this solved the problem.

 

Another solution is to detect if OS is XP (since it only happens in XP).

If so then detect when property editor object has an image and a text below (In the code below, for example, I distinguish if it is PointSize and LineWidth which they have not an image, only text so only text will be drawn on the cell by using TextRenderer.DrawText), and if so you can create an image and draw into it the image and the text below by using DrawToBitmap and then the text by using TextRenderer.DrawText respectively. DrawText is not affected by this issue.

 

Below the code (This method should be called from paint method).

 

 private void CreateCellImageXP(
                PropertyEditor propertyEditor,
                Graphics graphics,
                Rectangle clipBounds,
                Rectangle cellBounds,
                int rowIndex,
                DataGridViewElementStates cellState,
                object value,
                object formattedValue,
                string errorText,
                DataGridViewCellStyle cellStyle,
                DataGridViewAdvancedBorderStyle advancedBorderStyle,
                DataGridViewPaintParts paintParts)
            {
                // Create a Bitmap image in memory and set its CompositingMode
                Bitmap bmp = new Bitmap(cellBounds.Width, cellBounds.Height,PixelFormat.Format32bppArgb);                
                Graphics gBmp = Graphics.FromImage(bmp);

                gBmp.SmoothingMode = SmoothingMode.AntiAlias;
                gBmp.InterpolationMode = InterpolationMode.HighQualityBicubic;
                gBmp.PixelOffsetMode = PixelOffsetMode.HighQuality;

                Control ctrlValue = propertyEditor.Controls[0];

                float height = cellBounds.Height;

                int left = 0;
                int top = 0;

                if (propertyEditor.Source.PropertyName != "PointSize" &&
                    propertyEditor.Source.PropertyName != "LineWidth")
                {
                    // SET THE IMAGE                    
                    ctrlValue.Height = cellBounds.Height - 3;
                    Rectangle rectValue = ctrlValue.Bounds;

                    Bitmap imageValue = new Bitmap(rectValue.Width, cellBounds.Height, PixelFormat.Format32bppArgb);

                    ctrlValue.DrawToBitmap(imageValue, new Rectangle(0, 0, rectValue.Width, cellBounds.Height));
                    
                    //imageValue.Save("c:\\image.png", ImageFormat.Png);
                    gBmp.DrawImage(imageValue, new Rectangle(1, 1, rectValue.Width, cellBounds.Height));

                    height = ctrlValue.Height;
                    left = ctrlValue.Width + ctrlValue.Left + 5;
                    top = ctrlValue.Top;
                }
         
                // SET THE TEXT following the image
                StringFormat stringFormat = new StringFormat();                
                stringFormat.LineAlignment = StringAlignment.Center; // Vertical Aligment
                RectangleF rectf = new RectangleF((float)ctrlValue.Width + ctrlValue.Left + 5, (float)ctrlValue.Top, (float)cellBounds.Width, height);

                TextRenderer.DrawText(
                    gBmp, 
                    propertyEditor.Controls[1].Text, 
                    cellStyle.Font,
                    new Rectangle(left, top, cellBounds.Width, (int)height),                    
                    cellStyle.ForeColor,
                    cellStyle.BackColor, 
                    TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
 
                gBmp.Flush();                                

                // draw the bitmap on the cell
                graphics.DrawImage(bmp, cellBounds.Location);
                
                
                bmp.Dispose();
                gBmp.Dispose();                
            }

 Also, I have been creating a little program (see attachment), better than the one provided in the another thread. In this program I detect the OS in paint method. If XP I create a custom image to show it correctly by calling the method CreateCellImageXP. If not XP, then I call another method CreateCellImageVista7. These methods are called from paint method depending on the OS.

 

Now, by using your solution, only one method is always called for any OS (XP, Vista or 7), and it is named createCellImage (In the code is initially commented, you can try it by uncomment it and also uncomment the return that appears below) and there I apply what you has commented, textbox multiline to true.

 

Below I attach the program.

 

Thanks a lot for helping me.

 

PD.: I was doing this little program for you to check the issue but I come late... sorry. You was faster than me 😉 It also shows how to display notify error icon in a concrete cell when you entered an invalid value for the cell, check to enter a value greater than 20 in Line Width column, also tooltip implemented when mouse over the icon.

Message 24 of 25
(3,091 Views)

Awesome solution integrating the PropertyEditor into the DataGridView.

 

Did you have any other open questions with this issue? I know you had a lot of posts, and I want to make sure we addressed all of them.

National Instruments
0 Kudos
Message 25 of 25
(3,082 Views)