473,624 Members | 2,561 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Sql Client;
using System.IO;
using System.Text;
using System.Data;

namespace ConsoleApplicat ion3
{
class Class1
{

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

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

BinaryWriter bw = new BinaryWriter(ne w FileStream(@"C: \Temp\Picture_" +
System.DateTime .Now.Hour.ToStr ing() + "_" +
System.DateTime .Now.Minute.ToS tring() + "_" +
System.DateTime .Now.Second.ToS tring() + ".bmp", System.IO.FileM ode.Create));
bw.Write((byte[])Comm.ExecuteSc alar());
bw.Close();
Conn.Close();
}
}
}
I hope you can help me with this!

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

if (reader.read())

{

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

Bitmap bm = new Bitmap(ms);

bm.Save(your_fi lename);

}

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
623
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 way to efficiently retrieve string data. The best you can do is either allocate a static buffer of 64K (max size for a blob) or some smaller buffer that may not actually be able to contain all the data.
6
2233
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 when I do the following Byte byteBLOBData = new Byte; byteBLOBData = (Byte)(ds.Tables.Rows);
1
8684
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 MySQLSelectCommand(...; //select that row and column where is BLOB string dest = Server.MapPath("image.jpg"); FileStream binFile = new
3
1706
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 loading the image in a picturebox and it also works but there's a black line drawn from coordinates(0,0) to coordinates(image.width,0) when I look at the blob field using crystal reports 9, the line normaly isn't there.What am I doing wrong? Dim ms...
2
308
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 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;
5
16105
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 to an access database and I can save the data back to a file using getchunk and everything works correctly). The issue is that I am now trying to access the database in VB.NET 2003 through an OleDBDataAdapter (Jet 4.0 connection) and can't get it...
4
5086
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 field working fine, but the user 'saves' the file from the Binary field to a binary disk file and can then open it. It would be better for them to be able to just open the file directly for viewing etc. I can 'trigger' a load for the file myself, but...
0
2391
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 can't save the jpg file as a BLOB field using this Method (I'm calling a Stored Procedure): Private Sub save_BLOB(ByVal Photo As Byte())
9
7976
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 binary data into an Oracle BLOB column via ADO.NET. since my files are larger than 33k, it seemed had to use this technique: http://support.microsoft.com/default.aspx?scid=kb;en-us;322796
0
8231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8168
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8614
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8330
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7153
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2603
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.