Resize the image automatically for printing in ASP.NET

Post date: Jun 30, 2011 2:45:26 AM

1. HTML and JavaScript code for back button in to go previous pages

<INPUT TYPE=”button” VALUE=”Back” onClick=”history.go(-1);”>

This code will display one button and you can change the value parameter to display any text on the button. Here is the button to take you back to previous page. Note that the onClick event can be set to history.go(-3); to move back three steps. We can use +2 also to move forward two steps.

 

Using JavaScript

We can use JavaScript to create a link to take us back to previous or history page. Here is the code to move back the browser using client side JavaScript.

<a href = “javascript:history.back()”>Back to previous page</a>

2.Re-size the image based on desktop screen and A4 sheet:

Scenario:One of my assignment I have to show all the images captured by specific photographer using grid view,then user will select some images which he/she wants to print, I need to show those selected images in the separate page for printing.

Requirement:

All selected images should print within single A4 size paper. It should not exceed single page. Maximum I can select 21 images per page.

Solution:

Detail.aspx:

When ever I click Print button, it will call this function. This function helps me to find the selected images from the grid view check box and store those image details in the array list.

protected void PrintImage(object sender, EventArgs e)

{

lblResult.Visible = false;

ArrayList imgList = new ArrayList();

foreach (DataListItem item in DL_Vhcl_Img.Items)

{

HtmlInputCheckBox chkImg = item.FindControl(“chkBox”) as HtmlInputCheckBox;

if (chkImg != null) {

if (chkImg.Checked)

{

imgList.Add(chkImg.Value);

}

}

}

if (imgList.Count > 0)

{

//store the image details in the array List

Context.Items.Add(“img”, imgList);

//Transfer the control to print.aspx page to show those images

Server.Transfer(“print.aspx”, true);

}

else

{

lblResult.Visible = true;

}

}

Print.aspx:

 

<input type =”button” value=”Back” onclick=”history.go(-1);” style=”margin:10px” />

<form id=”form1″ runat=”server”>

<div style=”width:800px; height:600px”>

<% ArrayList imgList = (ArrayList) Context.Items["img"];

int imgCount = imgList.Count;

int maxWidth = 800;

int maxHight = 600;

int width =0;

//start adjusting the width, don’t bother about height, it automatically re size

if (imgCount == 1)

{

width = maxWidth;

}

else if (imgCount == 2)

{

width = maxWidth / 2;

}

else if (imgCount == 3 || imgCount == 4)

{

width = maxWidth / 2;

}

else if (imgCount <= 6)

{

width = maxWidth / 3;

}

else if (imgCount <= 9)

{

width = maxWidth / 3;

}

else if (imgCount <= 12)

{

width = (maxWidth / 4)-5;

}

else if (imgCount <= 15)

{

width = (maxWidth / 5) – 5;

}

else if (imgCount <= 18)

{

width = (maxWidth / 6) – 5;

}

else if (imgCount < 21)

{

width = (maxWidth / 7) – 7;

}

else

{

width = 0;

}

if (width == 0 )

{ %>

<h2 style=”margin:auto;color:Red” >It will support maximum 21 images to print</h2>

<%}

else

{

foreach (string str in imgList)

{

%>

<img src=’<%= str %>’ alt=”Images” width=’<%=width %>px’ style=”display:inline; margin:5px”/>

<%}

} %>

</div>

-Original Post http://greatindiaclub.oliwy.net/?p=451.