Saturday, April 9, 2011

Problem: Call an aspx page and have it decrypt and return an image

Impact: If you are working with images which are encrypted and stored in a folder. You cannot show the images directly in your aspx page as they are encrypted, by calling another aspx page you can open the encrypted image, decrypt it and return it back to the requesting page.

Solution:

1. Create an aspx page, name it DecryptImage.aspx and copy the code below.

protected void Page_Load(object sender, EventArgs e)
{
    string filePath = Page.Request.QueryString["filePath"];
    Response.ContentType = "image/jpg";
    FileStream encFile = new FileStream(filePath, FileMode.Open, FileAccess.Read);

    //Code to decrypt image goes here

    Response.BinaryWrite(decFileBytes);
    Response.Flush();
    Response.End();
}

2. Then in the calling page use an asp image control to display the image and set its url in the code behing the page file(the .cs file).

Example: 

<asp:Image ID="Image1" runat="server"></asp:Image>

protected void Page_Load(object sender, EventArgs e)
{
    Image1.ImageUrl = "DecryptImage.aspx?filePath=folderName/fileName.jpg";
}

No comments:

Post a Comment