Asked by:
2 diff blob format reading error

Question
-
I have 2 diff. image format stored in db2 database as blob one start with 0000000C6A5020200D0A87 which cannot be displayed in picture box I get error invalid parameters and the other start with FFD8FFE000104A4649460 with no problem.
So, I tried more than 1 solution till I finally succeeded to display both of them as in the below code
But I got both as in Pic. How can I display both Pic. In the right format?
Solution1
OleDbDataAdapter1.Fill(DataSet11, "PORT_IMAGE")
Dim c As Integer = DataSet11.Tables("PORT_IMAGE").Rows.Count
If c > 0 Then
Dim output = New Bitmap(Width, Height)
Dim ImageBytes() As Byte = DataSet11.Tables("PORT_IMAGE").Rows(c - 1)("P_IMAGE")
Dim bitmap As New Bitmap(Width, Height, PixelFormat.Format64bppArgb)
Dim bitmap_data = bitmap.LockBits(New Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format64bppArgb)
Marshal.Copy(ImageBytes, 0, bitmap_data.Scan0, ImageBytes.Length)
Dim i As Integer = 0
Do While (i < ImageBytes.Length)
Dim R As Byte = ImageBytes(i)
Dim G As Byte = ImageBytes((i + 1))
Dim B As Byte = ImageBytes((i + 2))
Dim A As Byte = ImageBytes((i + 3))
ImageBytes(i) = B
ImageBytes((i + 1)) = G
ImageBytes((i + 2)) = R
ImageBytes((i + 3)) = A
i = (i + 4)
Loop
bitmap.UnlockBits(bitmap_data)
Dim imge = CType(bitmap, Image)
PictureBox1.Image = imge
PictureBox1.Refresh()
'Format32bppArgb
Solution 2
Dim output = New Bitmap(Width, Height)
Dim rect = New Rectangle(0, 0, Width, Height)
Dim bmpData = output.LockBits(rect, ImageLockMode.ReadWrite, output.PixelFormat)
Dim ptr = bmpData.Scan0
Marshal.Copy(b, 0, ptr, b.Length)
output.UnlockBits(bmpData)
PictureBox1.Image = output
Solution3
Dim buffer() As Byte = DataSet11.Tables("PORT_IMAGE").Rows(c - 1)("P_IMAGE")
Dim bitmap As New Bitmap(Width, Height, PixelFormat.Format32bppRgb)
Dim bitmap_data = bitmap.LockBits(New Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb)
Marshal.Copy(buffer, 0, bitmap_data.Scan0, buffer.Length)
bitmap.UnlockBits(bitmap_data)
Dim imge = CType(bitmap, Image)
PictureBox1.Image = imge
- Edited by gwafy Tuesday, November 3, 2020 6:59 PM
Tuesday, November 3, 2020 4:14 AM
All replies
-
Hi gwafy,
Thank you for posting here.
According to your description, I have a question to confirm with you.
>>I finally succeeded to display both of them as in the below code. But I got both as in Pic. How can I display both Pic. In the right format?
What do you mean that 'got both as in Pic'?
Besides, here's an example of reading blob image data from Database, and you can take a look.
Dim cmdTxt As String = "SELECT something FROM yourtable WHERE Id = @id" Using Conn As OleDbConnection = New OleDbConnection(ConnString) Using cmd As OleDbCommand = New OleDbCommand(cmdTxt, Conn) cmd.Parameters.Add("@id", OleDbType.Integer).Value = 1 Conn.Open() Using rdr As OleDbDataReader = cmd.ExecuteReader() If rdr.Read() Then Dim ext As String = rdr.GetString(2) Using ms As MemoryStream = New MemoryStream(CType(rdr("imgData"), Byte())) PictureBox1.Image = Image.FromStream(ms) End Using End If End Using End Using End Using
Hope it could be helpful.
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Wednesday, November 4, 2020 6:55 AM -
HI Xingyu Zhao ,
thank you for your reply but I am so sorry it give me the following error
"Index was outside the bounds of the array."<o:p></o:p>
I am already work with this code to get my picture with no error
OleDbSelectCommand1.CommandText = "SELECT P_IMAGE FROM xxx WHERE id=(" & Text1.Text & ") "
OleDbSelectCommand1.Connection = OleDbConnection1
DataSet11.PORT_IMAGE.Clear()
OleDbConnection1.Open()
Try
OleDbDataAdapter1.Fill(DataSet11, "PORT_IMAGE")
Dim c As Integer = DataSet11.Tables("PORT_IMAGE").Rows.Count
If c > 0 Then
Dim buffer() As Byte = DataSet11.Tables("PORT_IMAGE").Rows(c - 1)("P_IMAGE")
Dim stmBLOBImage As New MemoryStream(buffer, 0, buffer.LongLength.ToString)
PictureBox1.Image = System.Drawing.Image.FromStream(stmBLOBImage, True, True)but I changed to this code to get the other picture which give me error "invalid parameteres"
Dim bitmap As New Bitmap(Width, Height, PixelFormat.Format32bppRgb)
Dim bitmap_data = bitmap.LockBits(New Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb)
Marshal.Copy(buffer, 0, bitmap_data.Scan0, buffer.Length)
bitmap.UnlockBits(bitmap_data)
Dim imge = CType(bitmap, Image)
PictureBox1.Image = imgein this code both images display as one line
first image is B & W start with "FFD8FFE000"
second image is color cant display it start with "0000000C6A50"
the 2 images is blob structure in db db2
Wednesday, November 4, 2020 5:13 PM -
https://stackoverflow.com/questions/48500822/how-to-handle-error-for-response-type-blob-in-httprequest
I hope answer the question please mark as accept if issue resolved
Wednesday, November 4, 2020 5:17 PM -
Thank you
I don t need to handle the error
I have 2 code
one display the first image W&B with no error but the color image give invalid parameters
i changed this code to display the 2 images but it is not working right
my db db2 windows application not web vs2010
- Edited by gwafy Wednesday, November 4, 2020 7:33 PM
Wednesday, November 4, 2020 7:17 PM -
Hi gwafy,
Thanks for your feedback.
>>I am already work with this code to get my picture with no error
If it works for you, why do you need to use LockBits to handle the image pixels?
We are waiting for your update.
Besides, here's a reference about using LockBits, and you can take a look.
Manipulate image pixels very quickly using LockBits in VB .NET
Best Regards,
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Thursday, November 5, 2020 2:10 AM -
Hi Xingyu Zhao
thanks for your reply
but my problem with the other image it gives me invalid parameters.
I select blob image from db2 database this blob have 2 different format
my column contain 2 blob format image
1- One of them start with "FFD8FFE000" B&W which I can select it without any problem.
2- The other format start with "0000000C6A50" it is color which gives an error " invalid parameter".
So, I changed my code to LockBits to display both images format, but the image display as line in picturebox
i need help to display different picture format without error.
- Edited by gwafy Thursday, November 5, 2020 3:49 AM
Thursday, November 5, 2020 3:46 AM -
Hi gwafy,
I create BitMap by the MemoryStream and the following code works for me.
Dim ImageBytes As Byte() = ... Dim ms As MemoryStream = New MemoryStream(ImageBytes) Dim Bitmap As Bitmap = New Bitmap(ms) LockBitmap(Bitmap) UnlockBitmap(Bitmap) PictureBox1.Image = CType(Bitmap, Image)
'LockBitmap' and 'UnlockBitmap' method is provided in the reference.
Public g_RowSizeBytes As Integer Public g_PixBytes() As Byte Private m_BitmapData As BitmapData Public Sub LockBitmap(ByVal bm As Bitmap) ' Lock the bitmap data. Dim bounds As Rectangle = New Rectangle( 0, 0, bm.Width, bm.Height) m_BitmapData = bm.LockBits(bounds, Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format24bppRgb) g_RowSizeBytes = m_BitmapData.Stride ' Allocate room for the data. Dim total_size As Integer = m_BitmapData.Stride * m_BitmapData.Height ReDim g_PixBytes(total_size) ' Copy the data into the g_PixBytes array. Marshal.Copy(m_BitmapData.Scan0, g_PixBytes, 0, total_size) End Sub Public Sub UnlockBitmap(ByVal bm As Bitmap) ' Copy the data back into the bitmap. Dim total_size As Integer = m_BitmapData.Stride * m_BitmapData.Height Marshal.Copy(g_PixBytes, 0, m_BitmapData.Scan0, total_size) ' Unlock the bitmap. bm.UnlockBits(m_BitmapData) ' Release resources. g_PixBytes = Nothing m_BitmapData = Nothing End Sub
Hope it could be helpful.
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- Edited by Xingyu ZhaoMicrosoft contingent staff Thursday, November 5, 2020 7:16 AM
Thursday, November 5, 2020 7:12 AM -
Files like .jpg starts with FFD8FFE0.
Maybe your second cell does not contain a valid or supported image. Do you know the original type or extension of this image? For example, if it is a special JPEG2000, then it does not seem supported by Bitmap and Image classes, but you can search for some libraries: https://www.bing.com/search?q=.net+jpeg2000.
Thursday, November 5, 2020 8:30 AM -
Hi Xingyu Zhao,
thanks for your reply
I try with your code but the problem not solve with the color image it give me
ArgumentExption "Parameter is not valid"
ERROR LINE Dim Bitmap As Bitmap = New Bitmap(ms)
2- The other format start with "0000000C6A50" it is color which gives an error " invalid parameter".
Thursday, November 5, 2020 10:44 AM -
Hi gwafy,
Try resetting the current location in the MemoryStream.
... Dim ms As MemoryStream = New MemoryStream(ImageBytes) ms.Seek(0, SeekOrigin.Begin) Dim Bitmap As Bitmap = New Bitmap(ms)
new Bitmap(ms) is going to read the data from the stream's current position - if the stream's current position is at the end of the data, it cannot read anything.
Best Regards
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Friday, November 6, 2020 6:10 AM -
Hi Viorel
Thank you for your support
you are right it is JPEG2000
the first 65 string in image start with
0000000C6A5020200D0A870A00000014667479706A703220000000006A7032200000002D6A703268000000166968647200000280000001E00003070700000000000
Saturday, November 7, 2020 6:58 PM -
Hi Xingyu Zhao
Thank you for your effort
I try with the last code I got the same error invalid parameters
Saturday, November 7, 2020 7:11 PM -
Hi gwafy,
As Viorel_ suggested, you need to use some other libraries to decode it.
Check the following reference.
You can convert the C# code to vb.net, and if you have any difficulty in converting code, please let me know.
Hope it could be helpful.
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Monday, November 9, 2020 3:03 AM -
Hi Xingyu Zhao.,
Thanks a lot for your support.
Actually I convert the c code to VB as following
Dim imge As New System.Drawing.Bitmap(700, 1500)
Dim g As Graphics = Graphics.FromImage(imge)
but I have a problem which is how I get data from DB and display it in datagridview or picturebox using the FreeImage Tools?
Best Regards.,
Gehan Wafy
Tuesday, November 10, 2020 7:02 PM -
Hi,
>>I have a problem which is how I get data from DB and display it in datagridview or picturebox using the FreeImage Tools.
Since 'FreeImage' is a third-party product, you can consider posting your question in the following forum.
Thank you for your understanding.
Best Regards,
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Wednesday, November 11, 2020 3:17 AM -
Hi Xingyu Zhao
Thanks you for your support.
i need help to populate image to rows after convert it from bytearry to image using freeimage
when display in data gridview i get only one image from 860 row all image display as x red wthi the "NAME" column
this is my code
OleDbSelectCommand2.CommandText = "SELECT P_IMAGE,NAME FROM PORTLOB"
OleDbSelectCommand2.Connection = OleDbConnection1
DataSet11.PORTLOBN.Clear()
OleDbConnection1.Open()
'Try
OleDbDataAdapter2.Fill(DataSet11)
Try
Dim c As Integer = DataSet11.Tables("PORTLOBN").Rows.Count
If c > 0 Then
Dim jpegBytes() As Byte = DataSet11.Tables("PORTLOBN").Rows(c - 1)("P_IMAGE")
Dim jpegStream As MemoryStream = New MemoryStream(jpegBytes)
Dim jpegDib As FIBITMAP = FreeImage.LoadFromStream(jpegStream)
jpegStream.Position = 0
Dim jpegBitmap As FreeImageBitmap = New FreeImageBitmap(jpegStream)
FreeImage.UnloadEx(jpegDib)
Dim img As Image = jpegBitmap
Dim imageColumn As New DataColumn
imageColumn.ColumnName = "col1"
imageColumn.DataType = GetType(System.Drawing.Image)
DataSet11.Tables("PORTLOBN").Columns.Add(imageColumn)
Dim dr As DataRow = DataSet11.Tables("PORTLOBN").NewRow
dr("col1") = img
DataSet11.Tables("PORTLOBN").Rows.Add(dr)
DataSet11.Tables("PORTLOBN").AcceptChanges()
Dim dgvImageColumn As New DataGridViewImageColumn
dgvImageColumn.DataPropertyName = "col1"
dgvImageColumn.Name = "col1"
dgvImageColumn.ImageLayout = DataGridViewImageCellLayout.Zoom
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
DataGridView1.RowTemplate.Height = 100
DataGridView1.Columns.Add(dgvImageColumn)
DataGridView1.DataSource = DataSet11.Tables("PORTLOBN")
DataGridView1.Columns("P_IMAGE").Visible = False
End If
Catch Ex As OleDb.OleDbException
MessageBox.Show(Ex.Message)
End Try
OleDbConnection1.Close()
End Sub
Sunday, December 6, 2020 12:29 PM