Crystal Report with Image

Pages: 5 (551 words) Published: May 25, 2006
Create a dataset using the fields you want to display in crystal report. Define the image field as hexaBinary datatype.

Create the Crystal report using this dataset.

Now Copy this Code where you want to call the report

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;

DataSet data = new DataSet();

private void PicureFromSQLDatabase()
{
int BUFFER_LENGTH = 152768; //' chunk size
string myConnString = "SERVER=ARUN;DATABASE=Northwind;uid=sa;pwd=;"; SqlConnection cn = new SqlConnection(myConnString);
string str_query = "SELECT * FROM Categories WHERE CategoryID > 8"; SqlConnection cn1 = new SqlConnection(myConnString);
SqlDataReader reader;
SqlCommand cmd=new SqlCommand();
cn.Open();
cmd.CommandText=str_query;
cmd.Connection=cn;
reader=cmd.ExecuteReader();
data.Tables.Add();

data.Tables[0].Columns.Add("Name", System.Type.GetType("System.String")); data.Tables[0].Columns.Add("Picture", System.Type.GetType("System.Byte[]")); int sNo = 1;
float targetWidth = 0;
float targetHeight = 0;
float imageWidth = 0;
float imageHeight =0;
float widthRatio = 0;
float heightRatio =0;
float optimalRatio = 0;
int xImgBoundary=0;
int yImgBoundary=0;
int widthImgBoundary=0;
int heightImgBoundary=0;
byte[] bitmapData = new byte[2000];
while (reader.Read())
{
SqlCommand cmdGetPointer = new SqlCommand("SELECT @Pointer=TEXTPTR(Picture), @Length=DataLength(Picture) FROM " +
"Categories WHERE CategoryName='" + reader["CategoryName"].ToString() + "'", cn1); SqlParameter PointerOutParam = cmdGetPointer.Parameters.Add("@Pointer", SqlDbType.VarBinary, 100); PointerOutParam.Direction = ParameterDirection.Output;

SqlParameter LengthOutParam = cmdGetPointer.Parameters.Add("@Length", SqlDbType.Int); LengthOutParam.Direction = ParameterDirection.Output;
cn1.Open();
cmdGetPointer.ExecuteNonQuery();
if (PointerOutParam.Value == DBNull.Value)
{
cn1.Close();
}
SqlCommand cmdReadBinary = new SqlCommand("READTEXT Categories.Picture @Pointer @Offset @Size HOLDLOCK", cn1); SqlParameter PointerParam = cmdReadBinary.Parameters.Add("@Pointer", SqlDbType.Binary, 16); SqlParameter OffsetParam = cmdReadBinary.Parameters.Add("@Offset", SqlDbType.Int); SqlParameter SizeParam = cmdReadBinary.Parameters.Add("@Size", SqlDbType.Int); SqlDataReader dr;

System.IO.FileStream fs = new System.IO.FileStream("c:\abc.jpg", System.IO.FileMode.Create, System.IO.FileAccess.Write); int Offset = 0;
OffsetParam.Value = Offset;
byte[] Buffer;
Buffer = new byte[BUFFER_LENGTH - 1];
while (Offset < (int)LengthOutParam.Value)
{
PointerParam.Value = PointerOutParam.Value;
if ((Offset + BUFFER_LENGTH) >= (int)LengthOutParam.Value)
{
SizeParam.Value = (int)LengthOutParam.Value - Offset;
}
else
{
SizeParam.Value = BUFFER_LENGTH;
}
dr = cmdReadBinary.ExecuteReader(CommandBehavior.SingleResult);
dr.Read();
dr.GetBytes(0, 0, Buffer, 0, (int)SizeParam.Value);
dr.Close();
fs.Write(Buffer, 0, (int)SizeParam.Value);
Offset += (int)SizeParam.Value;
OffsetParam.Value = Offset;
}
fs.Close();
string imgPath = "c:\abc.jpg";
Image img = System.Drawing.Image.FromFile(imgPath);
targetWidth = 2000;
targetHeight = 963;
imageWidth = img.Width;
imageHeight = img.Height;
widthRatio = targetWidth / imageWidth;
heightRatio = targetHeight / imageHeight;
optimalRatio = Math.Min(widthRatio, heightRatio);
xImgBoundary = (int)((targetWidth - ((optimalRatio * imageWidth)*0.9)) / 2); yImgBoundary = (int)((targetHeight - ((optimalRatio * imageHeight)*0.9)) / 2); widthImgBoundary = (int)((optimalRatio * imageWidth)*0.9);

heightImgBoundary = (int)((optimalRatio * imageHeight)*0.9);

Rectangle targetRect = new Rectangle(xImgBoundary,yImgBoundary,widthImgBoundary,heightImgBoundary); Bitmap bmp = new Bitmap((int)targetWidth, (int)targetHeight); Graphics g =...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Report Essay
  • Essay about Crystal Report
  • Mirror Image: Annorexia Report Research Paper
  • Images Essay
  • Essay about Image
  • the image Essay
  • image Research Paper
  • Essay on Image and Sound

Become a StudyMode Member

Sign Up - It's Free