How to Convert ORACLELOB object to Image File using C#? - c#

I have an Oracle Database Table where an image is stored in LONG Data Type. I want to retrieve this as an image file(Tiff file).
According to my research, LONG data column has to be first converted to LOB object and then into image file.
By using OracleClient library and TO_LOB function I have been able to receive ORACLELOB object from the database. However, I have not been able to convert this ORACLELOB object to an image file.
Any idea on how to do this?

Related

How to convert BLOB data stored in SQLite database into the SharpMap.Geometries.Geometry

I have geometry data stored in the SQLite database, and datatype of column is BLOB.
I used the below method to convert the BLOB data into the sharp map geometry
SharpMap.Converters.WellKnownBinary.GeometryFromWKB.Parse(< byte array >);
But it throws the exception "Byte order not recognized"
But if i use the sql server management studio database and save the geometry data in table using Image datatype then the i got the geometry without any error.
The only difference is of data type, in SQLite i have used data type "BLOB" and in sql server i have used the datatype "Image".
I also notice one more difference in size of byte array, if i use the SQLite database then i got the byte array size as "3812" and if i use the sql server database then i got the byte array size as "1902".
Does anybody knows the solution ?
I am working on below data :
0x01030000000100000076000000908FC85763BD2D41B5E1CEFAB9385841A8564425CABB2D412642A24FB83858411BFBC2F5D3BA2D4184A995ABBF385841CAD4637804BA2D415C8C795FA4385841F114538DDDB92D41F55448C2AC385841C64F734F30B92D41B249DC25CD385841A3CB7AAD4DB12D4199B5872A3B3858416D582E7839B02D41A9556428F637584143144991BBB02D416A06CFA4D53758417B22EADD21B12D41655331199F375841452E9D966AB12D4166ED16B88B3758419BB6676466B02D413D6180586D375841E89DECDF98B02D4191927111613758411C78DF662DB22D4170EA69F128375841D9D525280FB22D41A078FBDB103758415C485630A6B12D41F3EB2128EC3658419D97CCE77BB22D41C592B7E1CF365841831F9ECFB8B32D41589F8359E8365841A4599CA91AB42D4183458EF6F7365841BD50CAAF3AB42D41D8584F2DF836584115C52BF88AB42D41CB916AC0DF365841C9C54DE07BB32D41EE6958C1BF365841DE5A1651E3B22D415B4704FDA6365841D73CCC798FB22D41CB9E9E17963658415663F51F78B22D41E3403AA988365841B4235ECA76B22D41F0CA34EE7A36584188D446ECB9B32D41FB8ED8B5093658412812E3D8E8B32D41616C0DA2CC3558419DF34042BDB32D41EE235FC17B355841A36F009898B22D41054448484C355841E21AEBA9AFB42D41946B3A2761355841C463E28F4FB52D413FF3CA035F355841461A2E564BB52D4113CAB89257355841D2226CFEF8B62D41B71CC9EC153558419073855FC9B72D410A4B9247E1345841508668C7C4BC2D41E0B9D62FD7345841661D26E68ABE2D410E80C7D7AF34584149753FDA9AC02D418BDAADF075345841B50A90264DC32D41871EDC1F7D345841E0F2F43CA2C62D4107B6748781345841870AC042C0C62D41F61C18959D345841B238413BC0C72D4124BD21B4DC34584180D416141CC82D41E44457B4E934584175CA2F0700C92D4149867508343558414835F68B24CF2D418F8AD232F3345841D200559B07D02D414E65E5D5E1345841065A4DCB6CD02D4185CE4559E2345841254C3A15C5D02D415D1D78A14935584166C96D57F5D02D41C414E543623558415D6312F097D02D41C605342D94355841E65A332103D12D412A5960ABBA3558411194E61042D12D41F97012B6EC35584116DEF2091ED12D4186D8858FF03558412248444320D12D41EFAF11A6FD355841CB79634EEDD12D414275A2982D3658418C67CC2FE2D12D419E5D943153365841A93F65748BD12D418D280AC6643658419D1B2A62C0D12D41B19988E397365841934A32D73DD12D41BA8CCD9FAA365841646BF8D86DD22D414ED7D61EE136584146B4EFBE0DD32D417A3B6E9BE03658419F2A4C7030D32D41FCBCC386E53658413BB53AA930D32D4164780A6D1337584191D463BCD9D12D41B116E437083758419B2461F02CD12D415D301E26073758412FE24D704FD02D41DF24A4CB0C3758416BACD3FE50D02D411FFCC625173758413730503BD0CF2D41AAE124111C3758413061531920CF2D41A0D6E5442A3758411A6E1BE5A4CE2D414619DF4C3837584115D1D11FF4CC2D412A2B02E47B375841B0897DE34ECC2D41F7A6AFD36F3758418831353299CB2D4110590A15B03758414401543C3ACB2D4191DD8968C137584146EBF50830CA2D4158913B28D63758414ED056195EC92D4133420C50EF375841CF6C4EDAAAC82D41A57F0D5BF73758419E80ED363DC82D4120E370DA00385841A0D330BE85C72D4176B51AAC163858418132924D17C42D41ED5B97AB51385841DB1AC747F9C32D4163E87BEE4638584157EEAC6699C42D41C9D1BC2B30385841040137B0EFC42D4119CE642E2A3858417020BF9CB1C52D41BC74946A263858416A3227FE16C72D41489711A10E385841C9151581BDC92D414F1C52F8D637584124EAE0F5F3CA2D413D750DBCB237584147D5911AB2CB2D41B9E3E10F743758417635C66FB1CB2D4140FA886365375841632C30082CCA2D41CE0050B46437584105493CCA11CA2D413CB74FA156375841FE65D622B6CA2D414A1C58C131375841C950F9B0B5CA2D41B4443CB92B375841AD2913B768CB2D414719112023375841A9A4228E94CA2D4133F15AA90F375841AEE3024BA5C82D41EC089804EA365841F76822311FC82D41CC821BEE01375841DE1EB75E2AC62D418150AD64E0365841C039F774E3C42D41551E26A4CE3658414520FB2E0CC42D415933F721D336584151443641D7C32D41FE09F7AA0437584112C708BA1AC12D41F123AB9F14375841E933DCD86CBF2D412A142AE92A375841A3BBE95204BF2D41538E58422037584179B2EE56F2BD2D416FA23E89EF365841F8B58C69BFBD2D41387D57ABF4365841EABF0EE24EBD2D41C63A39B302375841E83C19224DBC2D410263BB2A333758411530B6EA54BC2D41C672B000853758413E00C81F41BC2D4171582088943758416914F51292BC2D41B3EC88989837584110B8EC661EBD2D41582E965DC13758416CE832F109BD2D41A36943A90C3858419923E1491BBD2D41161BD1B918385841E17A43A53ABD2D4126D39FFB2038584123EF3F59FEBD2D41EC01CAEC313858419D044C88A4BC2D410422669584385841908FC85763BD2D41B5E1CEFAB938584100000000
You have stored the hexadecimal representation of the value as text.
CSV files cannot contain blobs; you have to export the data in some other format. Try as SQL, and then convert the blob literal from the SQL Server format, i.e.:
INSERT INTO ... VALUES (... 0x0123...)
into the SQLite format, i.e.:
INSERT INTO ... VALUES (... x'0123...')
If CSV is your exchange format you should transform your geometry into a text representation using e.g. the .STAsText() function on the geometry.
When you import the CSV text (by writing own code) into your SQLite database you sould use a Well-Known-Text parser transforming that text inot a geometry and from there into a Well-Known-Binary blob that can be handled by SharpMap. This can be done using (e.g.) NetTopologySuite's Wkt reader:
var rdr = new NetTopologySuite.IO.WktReader();
var geom = rdr.Read(csvValue);
var blob = geom.AsBinary();
The issue is solved.
What happened is when i am storing the sql server geometry data in CSV file, it stores in the hexadecimal format. And when i am importing that data in SQLite, it stores geometry in hexadecimal format only not in the BLOB format.
So i search something like directly converting the sql server geometry data in the SQLite BLOB data.
So i found this link : https://www.codeproject.com/Articles/26932/Convert-SQL-Server-DB-to-SQLite-DB
It directly convert the sql server table into the SQLite table, so that the geometry data in sql server is automatically converts into the BLOB, and there is no issues while converting that BLOb data into the SharpMap Geometry.

C#: Use byte array to save PictureBox images to SQL Server database through a datatable object

I am learning C# including interacting with SQL Server databases. I am stuck however, on adding PictureBox images programmatically to a SQL Server database:
I have created a form with text fields, a picture box, to display/edit/add single records to a database successfully and am successfully using all of the following:
DB Connection = RRConnection
SqlCommand object = playersCommand
SqlDataAdapter object = playersAdapter
dataTable object = playersTable
currencyManager = playerManager
PictureBox1 control = including a button to navigate filesystem and select/load jpeg image into form PictureBox1
Bindings like - txtName.DataBindings.Add("Text", playersTable, "Name") for text fields - I think I need to bind the picturebox1 IMage Byte Array but do not know how to do
When the form closes, the table is loaded into the SQLServer database table tbl_BB into the column named "Image" of type image.
I know I have to convert the image to a byte array to save, but am confused on how to bind the image byte array to the dataTable, allowing it to be uploaded to the database on form close. I've done a lot of googling on it but the solutions do not seem to match the code I am using...
Given the above, can you advise the code necessary for this?
Don't use image it will be removed from MS SQL soon.
https://learn.microsoft.com/en-us/sql/t-sql/data-types/ntext-text-and-image-transact-sql
Just use varbinary.
the general way to do it is:
1) SQL Server will have a varbinary(max) column.
2) Take the jpeg and convert it to a byte array
3) add the byte array as a paramter to your sql command object
4) execute nonquery for insertion.
May I suggest you merely save the image on disk directly, and reference the path to the image in the DB instead? Saving images in a db is considered bad practice.

How do I include Images in my Database in Visual Studio 2010

I'm fairly new to C# .Net. We're being taught it at University and are using Visual Studio to create windows forms. As a new part to the subject we're using databases, tables and datasets.
I opened a new Windows Form project and immediately added a new database to it. The table I want to create will have 2 columns - ImageID and the image itself. In what way do i add the image in to the box? I've tried full path, relative path and dragging the image in, but whatever I do i get the same error message....
Invalid Value
The changed value in this cell was not recognized as being valid.
.Net Framework Data Type: Byte[]
Error Message: You cannot use the result pane to set this Field data to
values other than NULL
Type a value appropriate for the data type or press ESC to cancel the
change
How can I have images in there? I just don't know how to use the image data type within the table. Any help is much appreciated.
A simpler approach is to store the image in the file system and only its path in the database. Basically you define a base folder:
string baseFolder = "c:\Program Files\MyApp\Images";
And use it to store relative paths in the database:
INSERT INTO ImagesTable (Name, Path)
Values ('German Shepherd', 'Dogs\german-shepherd.jpg')
Then, when you need to retrieve the image, you can do it like this:
string path = Path.Combine(baseFolder, 'Dogs\german-shepherd.jpg');
Image img = Image.FromFile(path);
In the following SO question you can find more information about the pros and cons of this approach:
Should I store my images in the database or folders?
you can store images in sql server 2008. Just create database table having column datatype "image".
now from .net code use the file upload control to select the image file and then convert the image parameter into byte[] before inserting image data into the database.

How to store an image into database

How can I use openfiledialog and a picturebox and upload an image and store it into SQL Server
If you g0ogled c# store image in sql, the first 2 results would have been
Store or save images in SQL Server database using C#
Store or Save images in SQL Server
You need to keep field datatype as varbinary(Max) in the database table. then convert image into byte array. then pass this byte array as parameter respect to your varbinary field. it will be saved in your database. while retrieving, get value from database as byte array the convert it into Image object. hope this sequence helps you. or more code example you can find here

In Asp.net (c#) ,how to store an image in database(linq to sql)?

i need to get an image from client and displayed(preview) before saving it into database..and then should be saved in database after getting preview..?
Is is possible in Asp.net using c#... ?
Yes, it's possible.
If you are using MS SQL Server, declare a field in your table as type "image" or "varbinary". Varbinary limits the number of bytes stored depending on your version of SQL Server, so "image" is the way to go. After all, you are storing an image ;)
Then store your image as a byte array in that field.
Without seeing some code, it's hard to give code samples. Are you using SqlCommand directly, Linq2Sql, Entity framework or NHibernate to communicate with your database?
[Edit: Linq2Sql sample]
On your linq object you will have a propery called Image, if that's what you named your image/varbinary column. To assign it do this:
// assign data
byte[] imageByteArray = ...some byte data...;
myObject.Image = new Binary(imageByteArray);
// save to db
dataContext.YourTable.InsertOnSubmit(myObject); //YourTable is the name of your actual table class
dataContext.SubmitChanges();
where imageByteArray is a byte[] holding your image.
The Binary object is a wrapper around a byte[].

Categories