473,395 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How do I fetch a bmp from a BLOB and save it as a file?

Hi

I have a table with a BLOB column which contains bmp images. I'm
unsuccessfully trying to fetch one of these images and saving it as a file.
The result is a file with a size of 12Kb, but it's not a valid BMP file (I
can't view it in an image viewer). The image I try to fetch is quite small
though, so the size (12Kb) is sensible. I've pasted my code below (and it
doesn't throw any exception):

using System;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Data;

namespace ConsoleApplication3
{
class Class1
{

[STAThread]
static void Main(string[] args)
{
SqlConnection Conn = new SqlConnection();
Conn.ConnectionString = "Here I put my connection string";
Conn.Open();

SqlCommand Comm = new SqlCommand("SELECT Picture FROM TableName WHERE
Name='John'", Conn);

BinaryWriter bw = new BinaryWriter(new FileStream(@"C:\Temp\Picture_" +
System.DateTime.Now.Hour.ToString() + "_" +
System.DateTime.Now.Minute.ToString() + "_" +
System.DateTime.Now.Second.ToString() + ".bmp", System.IO.FileMode.Create));
bw.Write((byte[])Comm.ExecuteScalar());
bw.Close();
Conn.Close();
}
}
}
I hope you can help me with this!

Joakim
Jul 21 '05 #1
2 1585
Hi!
Try this:
reader = Comm.ExecuteReader();

if (reader.read())

{

System.IO.MemoryStream ms = new
System.IO.MemoryStream((byte[])reader["Scan"]);

Bitmap bm = new Bitmap(ms);

bm.Save(your_filename);

}

Hope that helps.

Best regards.
Jul 21 '05 #2
It turned out that my code was correct, but that the software that managed
the database automatically compressed the data in the BLOB column. After
disabling that compression, it worked fine.

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: AJ Shankar | last post by:
Hi, In the C API, is there any way to execute a query, store all the results, and somehow find the maximum realized width for each of the fields before fetching each row? Otherwise there is no...
6
by: Pierre-Benoit | last post by:
Hi there, I've a strange problem with ado.net and an Access db. I need to create a little C# app that take the content of "ole object" field and then save it into a file. The problem is that...
1
by: Markusek Peter | last post by:
Hi, I'm using MySQLDriverCS. I've got no problem to store BLOB into database, but I can't get it back(save to file). Problem is with DataTable(returns string:( ) My code: -- DataTable dt = new...
3
by: Peter Proost | last post by:
Hi I got this code to read in a file to the database, if use it to save a image from a picturebox to the database it works ok, but now I use it to directly save a file to the database without...
2
by: Joakim Olesen | last post by:
Hi I have a table with a BLOB column which contains bmp images. I'm unsuccessfully trying to fetch one of these images and saving it as a file. The result is a file with a size of 12Kb, but it's...
5
by: Mark | last post by:
I have a VB 6 program that I have saved an audio file to using DAO and the appendchunk as is shown at http://support.microsoft.com/kb/103257/EN-US/. ; I have it working fine in that app (it saves...
4
by: Bishman | last post by:
Hi, Can someone suggest the best technique / method of opening a Word ( or any other ) Document from an SQL BLOB ? I have the process of saving and retrieveing the file from an SQL Binary...
0
by: Big George | last post by:
Hello, I'm trying to save a jpg file of 300KB as a BLOB field in an Oracle 10g Database. If I try to call a Stored Procedure, it fails. If I use CommandText with SQL sentence, it success. I...
9
by: matt | last post by:
hello, im doing my first ASP.NET app that inserts & retrieves files from Oracle (no need for a discussion on *that*!). i learned first-hand of the somewhat awkward technique for inserting...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.