|
|||||||||||||||||||||||||
|
|||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis article describes an easy approach to converting a In one particular application, the intent was to dynamically produce engineering drawings within a batch process and each of those drawing was to contain a bill of materials. The bill of materials changed as each drawing in the batch job was produced, and for that reason, it was not possible to predetermine what would be contained within the bill of materials. Given it was not possible to add a grid control nor was it possible to predetermine what would be contained in the grid control, I opted to dynamically generate the bill of materials using a grid control, export the grid control’s content to a bitmap, and then embed the bitmap in the correct location within the drawing. This document will address the approach used to convert the grid control into a bitmap. Even though this document is focused on the use of a Getting StartedTo get started, unzip the sample code and project supplied with this article. Open the solution up in Visual 2005 and you should note that the application contains two significant files: frmMain.vb and DataGrid2Bitmap.vb. There are a couple of other files in the solution but they are not particularly important to this discussion. The frmMain.vb class does use a connection to my local copy of SQL Server, you may wish to update this connection string or just add a new connection to the project and bind the grid to that connection. You may also wish to take a look at the properties set on the grid, it is configured to not display selections (this is accomplished by changing the Default Cell Style such that the selected cell background color property matches the normal grid and the selected cell foreground color property matches the normal grid. The Rows Visible Headers property is also set to false to remove the left hand columns row selection and row pointer column. The The CodeOpen DataGrid2Bitmap.vb in the code window; the file is a code module and therefore it does not need to be instanced in order to use it. The DataGrid2Bitmap.vb file contains an import statement, it is: Imports System.Drawing.Imaging
This import is needed to process the bitmap and is required for this demonstration. After the module declaration, you should see this code: Private Declare Auto Function BitBlt Lib "gdi32.dll" _
(ByVal pHdc As IntPtr, ByVal iX As Integer, _
ByVal iY As Integer, ByVal iWidth As Integer, _
ByVal iHeight As Integer, ByVal pHdcSource As IntPtr, _
ByVal iXSource As Integer, ByVal iYSource As Integer, _
ByVal dw As System.Int32) As Boolean
Private Const SRC As Integer = &HCC0020
The declaration is used to supply the application with direct access to the gdi32.dll’s Following the constant declaration, you should see the following code: Public Sub ConvertDG2BMP(ByVal dg As DataGridView, _
ByVal sFilePath As String)
dg.Refresh()
dg.Select()
Dim g As Graphics = dg.CreateGraphics
Dim ibitMap As New Bitmap(dg.ClientSize.Width, _
dg.ClientSize.Height, g)
Dim iBitMap_gr As Graphics = Graphics.FromImage(ibitMap)
Dim iBitMap_hdc As IntPtr = iBitMap_gr.GetHdc
Dim me_hdc As IntPtr = g.GetHdc
BitBlt(iBitMap_hdc, 0, 0, dg.ClientSize.Width, _
dg.ClientSize.Height, me_hdc, 0, 0, SRC)
g.ReleaseHdc(me_hdc)
iBitMap_gr.ReleaseHdc(iBitMap_hdc)
If sFilePath = "" Then Exit Sub
ibitMap.Save(sFilePath, ImageFormat.Bmp)
End Sub
This function accepts two arguments, the first is the The rest of the code obtains a graphic context for the for the grid, creates a bitmap sized to match the grid, sets up for and executes the
Figure 1: Example Output – A bitmap from a grid The frmMain.vb class is used as a test harness for the module previously discussed. The frmMain.vb class has a menu with a save menu option. If you open up the save menu option’s handler, you will see this code: Try
'Open a file dialog for saving map documents
SaveFileDialog1.Title = "Save As BMP File"
SaveFileDialog1.Filter = "Bitmap File (*.bmp)|*.bmp"
If SaveFileDialog1.ShowDialog() = _
Windows.Forms.DialogResult.Cancel Then
Exit Sub
End If
Catch ex As Exception
Exit Sub
End Try
'Exit if no map document is selected
Dim sFilePath As String
sFilePath = SaveFileDialog1.FileName
If sFilePath = "" Then
Exit Sub
Else
DataGrid2Bitmap.ConvertDG2BMP(DataGridView1, sFilePath)
End If
The save menu item handler’s code opens up a standard save file dialog box after setting it to use the bitmap file extension. The path collected by this function is subsequently used to call the SummaryThis is a simple project; the only intent was to demonstrate a workable approach to converting a
|
||||||||||||||||||||||||