MySQL Blob Column Jpg Write,read and display

Post Reply
sanilpmc
Posts: 36
Joined: Tue Jun 17, 2008 7:09 am

MySQL Blob Column Jpg Write,read and display

Post by sanilpmc »

Hi,

Can somebody provide a sample code for writing and reading a jpg file to a blob field in MySQL using FWH Harbour

For writing to Blob filed, I tried

Code: Select all

cImageData:=MemoRead("D:\Test.Jpg")  // Read the Jpj file contents to a variable
cImageData:=cMimeEnc(cImageData)
Update the table's blob field with cImageData

To display the Image from the database, I prefer to read the blob field content directly into memory variable (ie without writing the blob contents to hard disk, to create a jpg file) and then display the image on the IMAGE control placed on my Dialog using oImage:LoadFromMemory(cImageData)

For reading and displaying on IMAGE Control, I use the following code.

Code: Select all

@ 0, 0 IMAGE oImage SIZE 150, 150 OF oDlg

Code: Select all

cImageData:=oRecSet:Fields("MyBlobColumn"):Value
cImageData:=cMimeDec(cImageData)

oImage:LoadFromMemory(cImageData)
oImage:Refresh()
But nothing displayed.

I have also tried HexToStr() and StrToHex() to write and read from MySQL Blob field but not successful
I have also tried fMimeEnc() and fMimeDec() to write and read from MySQL Blob field but not successful

Searched the forum and could not find any working solution.

TIA
sanilpmc
Posts: 36
Joined: Tue Jun 17, 2008 7:09 am

Re: MySQL Blob Column Jpg Write,read and display

Post by sanilpmc »

The problem occurs only when used with Harbour. With xHarbour it is working fine.

To write jpg file data to a Blob column in MySQL Table, I use the following code. This code is working fine

Code: Select all

cImageData:=MemoRead("D:\MyImage.Jpg")
cImageData:=cMimeEnc(cImageData)

// Update MySQL Blob column with cImageData
To read and display the Image file data from the Blob column, I use the following code. Unfortunately read and display code works fine in xHarbour and fails in Harbour.

Code: Select all

cImageData:=oRecSet:Fields("MyPhoto"):Value
cImageData:=cMimeDec(cImageData)

// Image Control
oImage:LoadFromMemory(cImageData)
oImage:Refresh()
I checked using cMimeEnc() and cMimeDec() with both Harbour and xHarbour and found that both these functions are working fine as expected.

Any idea what is wrong with the data retrieval from blob column using Harbour. ?

I don't have plans to use xHarbour for my project
sanilpmc
Posts: 36
Joined: Tue Jun 17, 2008 7:09 am

Re: MySQL Blob Column Jpg Write,read and display

Post by sanilpmc »

Code: Select all

// Blob Column. Data stored after cMimeEnc()
cStrData = oRecSet:Fields("Photo"):Value  

MsgInfo( ValType(cStrData) )
 
In Harbour the Valtype is returned as "A", whereas in xHarbour the valtype is "C"
The contents of the array (Harbour) are just numbers, where as in xHarbour it is a string of junk characters

This is the reason that the image is not displayed when using Harbour

Anybody any idea why is this difference in Harbour and xHarbour when the data is retrieved from a Blob Column. Any idea how to handle this case ?. Anybody here experienced similar problem ?

The original Jpg image size is less than 10kb in size

Harbour and xHarbour version used
Harbour ver 3.1.0dev (Rev. 17346)
xHarbour build 1.2.1 (SimpLex) (Rev. 9445)
Post Reply