Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

image from dataset in report 1

Status
Not open for further replies.

caudet

Programmer
Oct 2, 2003
15
0
0
CA
In a report containing many field I must include an image wich is traced with formulae and variable from a database. Since the image is different for each page of the report I decided that after tracing it with the GDI+ in C# I would include it in a dataset with the field from the report and 2 other field I populate in code.

So far thing are working nicely, the field from the database are printing correctly in the report. But the image and the field I populate in code are not printing on the report.

I assign the image in the database this way:

Graphics g;
g = Graphics.FromImage(imageBitmap);//blank bitmap
//tracing is done
dataSet.Tables["Order_cmd"].Rows["img"] = g;

, and for the text field:

dr_array[0]["og_diamMin"] = tempstr.Substring(0, 4) + "(" + ogDiamUtile + ")";

where dr_array is a array of datarow wich facilitate access to the corresponding row of each of the 3 tables in the dataset.

So now the report are printing(with no CrystalReportViewer, only a ReportDocument Object) but the field in the dataset I try to populate in code are not. Anyone know what I should do?

Thank you!

Charles
 
How did you stored the images in the database ? I think they are stored as blob fields. So, you retrieve the blob fields diferently than other fields.
Here is a an example for MSSQL how to retrieve the SqlDataType.Image objects
Code:
SqlConnection pubsConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=pubs;");
SqlCommand logoCMD = new SqlCommand("SELECT pub_id, logo FROM pub_info", pubsConn);

FileStream fs;                          // Writes the BLOB to a file (*.bmp).
BinaryWriter bw;                        // Streams the BLOB to the FileStream object.

int bufferSize = 100;                   // Size of the BLOB buffer.
byte[] outbyte = new byte[bufferSize];  // The BLOB byte[] buffer to be filled by GetBytes.
long retval;                            // The bytes returned from GetBytes.
long startIndex = 0;                    // The starting position in the BLOB output.

string pub_id = "";                     // The publisher id to use in the file name.

// Open the connection and read data into the DataReader.
pubsConn.Open();
SqlDataReader myReader = logoCMD.ExecuteReader(CommandBehavior.SequentialAccess);

while (myReader.Read())
{
  // Get the publisher id, which must occur before getting the logo.
  pub_id = myReader.GetString(0);  

  // Create a file to hold the output.
  fs = new FileStream("logo" + pub_id + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
  bw = new BinaryWriter(fs);

  // Reset the starting byte for the new BLOB.
  startIndex = 0;

  // Read the bytes into outbyte[] and retain the number of bytes returned.
  retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);

  // Continue reading and writing while there are bytes beyond the size of the buffer.
  while (retval == bufferSize)
  {
    bw.Write(outbyte);
    bw.Flush();

    // Reposition the start index to the end of the last buffer and fill the buffer.
    startIndex += bufferSize;
    retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
  }

  // Write the remaining buffer.
  bw.Write(outbyte, 0, (int)retval - 1);
  bw.Flush();

  // Close the output file.
  bw.Close();
  fs.Close();
}

// Close the reader and the connection.
myReader.Close();
pubsConn.Close();
-obislavu-
 
If I understand your code correctly you take bitmap from a database and save them as file.

In my problem I already have images in memory and I must put them in the dataset, effectively it is the reverse of what you explain. After some thinkering I've succeded in including the Image object in my dataset.

Graphics g;
Image I;
I = Image.FromFile([filepath]);
g = Graphics.FromImage(I);
//modif are made on the image with the Graphics obj
dataSet.Tables["v_OrdreFabrication_cmd"].Rows["dessin"] = (Bitmap)I;

After these steps I can clearly see the image in memory if I watch the memory during execution. After having loaded the report I try to set it dataset property this way:

report.SetDataSource(dataSet);

and I receive this error:

An unhandled exception of type 'System.InvalidCastException' occurred in system.data.dll

Additional information: Specified cast is not valid.

This error is clearly related to the image field in the dataset. If I don't assign a Image obj to the field the error doesn't appear and the report print correctly(minus the image). Anyone got a clue on what I do wrong? Don't hesitate to ask if you need more details.

Thanks!
 
The ["dessin"] column should have the SqlDbType as Image and in this case you can store in this column only Array of Byte type.
So you have to convert the Image object to an Array of Byte type and then store the result in the "dessin" column:
Code:
Byte [] img = ...
dataSet.Tables["v_OrdreFabrication_cmd"].Rows[i]["dessin"] = img;
-obislavu-
 
After having converted my Image into a stream and passing it to the byte array its work. Thanks!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top