Skip to main content Skip to footer

Export C1Gauge to Image

Many users have asked us whether it is possible to export C1Gauge as an image. Currently we don't have a direct method available for exporting gauges to images. However since this is a .NET based control, we can use standard methods to capture the image of this control and then save it. Using this method we can save C1Gauge as an image. C# Code

[System.Runtime.InteropServices.DllImport("gdi32.dll")]  
private static extern bool BitBlt(  
IntPtr hdcDest,  
int nXDest,  
int nYDest,  
int nWidth,  
int nHeight,  
IntPtr hdcSrc,  
int nXSrc,  
int nYSrc,  
int dwRop);  
public Bitmap CaptureControl(Control control)  
{  
     Bitmap controlBmp;  
     using (Graphics g1 = control.CreateGraphics())  
     {  
          controlBmp = new Bitmap(control.Width, control.Height, g1);  
          using (Graphics g2 = Graphics.FromImage(controlBmp))  
          {  
                IntPtr dc1 = g1.GetHdc();  
                IntPtr dc2 = g2.GetHdc();  
                BitBlt(dc2, 0, 0, control.Width, control.Height, dc1, 0, 0, 13369376);  
                g1.ReleaseHdc(dc1);  
                g2.ReleaseHdc(dc2);  
          }  
     }  
     return controlBmp;  
}

VB.Net Code

<System.Runtime.InteropServices.DllImport("gdi32.dll")> _  
    Private Shared Function BitBlt(hdcDest As IntPtr, nXDest As Integer, nYDest As Integer, nWidth As Integer, nHeight As Integer, hdcSrc As IntPtr, _  
 nXSrc As Integer, nYSrc As Integer, dwRop As Integer) As Boolean  
    End Function  
    Public Function CaptureControl(control As Control) As Bitmap  
        Dim controlBmp As Bitmap  
        Using g1 As Graphics = control.CreateGraphics()  
            controlBmp = New Bitmap(control.Width, control.Height, g1)  
            Using g2 As Graphics = Graphics.FromImage(controlBmp)  
                Dim dc1 As IntPtr = g1.GetHdc()  
                Dim dc2 As IntPtr = g2.GetHdc()  
                BitBlt(dc2, 0, 0, control.Width, control.Height, dc1, 0, 0, 13369376)  
                g1.ReleaseHdc(dc1)  
                g2.ReleaseHdc(dc2)  
            End Using  
        End Using  
        Return controlBmp  
    End Function

You can use the above function to get the Bitmap object when you pass the C1Gauge control as the parameter, which you can further save as file. Please refer to the Sample_ExportGauge for implementation of the above approach.

MESCIUS inc.

comments powered by Disqus