First off - thanks for all the information and snippets around here. Appreciate it. My problem;
I'm trying to save some datagridview data, including images, to an XML file. Then trying to read it back in the grid again. I'm using a dataset & table (unbounded) for easy XML writing as as far as I'm aware - binding doesnt work with an imagecolumn.
I can save the data, then read it in again. However - when I try to save the data again - it fails on the following line in "Sub Savetofile":
Img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
with the following error:
An unhandled exception of type System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll Additional information: A generic error occurred in GDI+.
Any ideas what I'm missing?
Private Sub SaveToFile(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
Dim rows As Integer = DataGridView1.Rows.Count - 1
Dim cols As Integer = DataGridView1.Columns.Count - 1
Dim MyByte As Byte() = Nothing
Dim Img As Image = Nothing
Dim ms = New MemoryStream()
DataSet1.Observations.Rows.Clear()
For i = 0 To rows
For j = 0 To cols
If j = 0 Then
Img = DataGridView1.Rows(i).Cells(j).Value
Img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
MyByte = ms.ToArray()
DataSet1.Observations.Rows.Add.Item(1) = Compress(MyByte)
ms.close()
ElseIf j >= 1 Then
If DataGridView1.Rows(i).Cells(j).Value IsNot Nothing Then
DataSet1.Observations.Rows(i).Item(j + 1) = DataGridView1.Rows(i).Cells(j).Value.ToString
End If
End If
Next
Next
File.Delete("C:\test2.quad")
DataSet1.WriteXml("C:\test2.quad")
End Sub
Private Sub OpenFile(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
Dim ms = New MemoryStream()
Dim MyByte As Byte()
DataSet1.Clear()
DataGridView1.Rows.Clear()
DataSet1.ReadXml("C:\test2.quad")
Dim rows As Integer = DataSet1.Observations.Rows.Count - 2
Dim cols As Integer = DataSet1.Observations.Columns.Count - 1
For i = 0 To rows
For j = 1 To cols
If j = 1 Then
MyByte = Decompress(DataSet1.Observations.Rows(i).Item(1))
Dim stream As New MemoryStream(MyByte)
DataGridView1.Rows.Add(Image.FromStream(stream))
stream.Close()
ElseIf j >= 2 And DataSet1.Observations.Rows(i).Item(j) IsNot Nothing Then
DataGridView1.Rows(i).Cells(j - 1).Value = DataSet1.Observations.Rows(i).Item(j).ToString
End If
Next
Next
End Sub