Thursday, March 18, 2010

Align Image Center in Page Header of RDLC file

Align Image Center
There is no automatic centering behavior for the Image control, you can simulate this by writing an expression for PaddingLeft.
Follow steps:
Step 01 :
Set Location property of the Image control to (0in, 0in).
Step 02:
Stretch the Image Control to full width of page header.
Step 03:
Set the Sizing property of the Image control to Clip
Step 04:
Create a new Parameter in Report say "ImageWidth" of datatype String.
Step 05:
Set Image Control Padding Left property to "=Parameters!ImageWidth.Value"
Step 06:
Now calculate value to set Left-Padding for Image Control,

//Calculate Left Padding for Image Control based on Image Size, passed as memory stream.

private string CalculateLeftPadding(byte[] imageData)
{
int DPI = 96; // DPI is typically 96

// Get Width of Image
int ImageWidthInPixels = System.Drawing.Image.FromStream(new System.IO.MemoryStream(imageData)).Width;

// Page Width of your RDLC file
double pageWidthInInches = 7; // By default i have set value to 7

//calculate the half of the difference between the width of the image and the width of the column.
double padding = (pageWidthInInches - ImageWidthInPixels / DPI) / 2;

// The padding property of Image Control takes a size, which is a string including the size and the units.
return Convert.ToString(Math.Round(padding, 2)) + "in";
}


Step 07:
Now just pass this value to your RDLC File --

string LeftPaddingForLogo = CalculateLeftPadding(imageData);
ReportParameter p1 = new ReportParameter("ImageWidth", LeftPaddingForLogo);
reportViewer.LocalReport.SetParameters(new ReportParameter[] { p1});

No comments: