0

I am working on a form Image.aspx and I want to upload an image, resize it and display it in a new window.

Here I am uploading the image,

UploadPath = Server.MapPath("../BrandPic/" + cmbImages.SelectedValue);
FileName = cmbImages.SelectedValue;
Bitmap OriginalBM = new Bitmap(UploadPath);

Here I am resizing it

int NewWidth = 1024;
int newHeight = 768;
Size newSize = new Size(NewWidth, newHeight);
Bitmap ResizedBM = new Bitmap(OriginalBM, newSize);

Here I am passing it

HttpContext.Current.Session["Pic"] = ResizedBM;

Here is my source code to open the image in new window

<a href='<%=Convert.ToString(HttpContext.Current.Session["Pic"])%>' rel="lightbox" >

But there is an error occured. Check the URL. So any idea to solve this issue ???

1 Answers1

1

ResizedBM is a Bitmap variable, not a path to a file that can be referenced in your html. To do what you want you'll likely have to save it out as a file and then reference the file that you saved in your href.

you could have a generic handler like resizedImage.ashx that you could pass the image id to. Then your link would be <a href='resizedImage.ashx?id=[your image id]' rel='lightbox'>link text</a>. That generic handler would resize the image and output the image to the browser

more info at MVC3 generic handler (.ashx) for Images resizing (Need clean URL)

Community
  • 1
  • 1
John Boker
  • 82,559
  • 17
  • 97
  • 130
  • I have tried this and its working but it takes lot of space as there are thousands of images. Actually I want to display image from the Bitmap variable without saving it. – Muhammad Rizwan Aug 21 '13 at 12:32
  • You might want to, but you will probably have trouble doing so. – John Boker Aug 21 '13 at 12:34
  • I am using Bitmap only to resize the image. Is there any other simple solution to resize it and display it through hyperlink? – Muhammad Rizwan Aug 21 '13 at 12:37
  • The main purpose of form is to display an image(in standard size) in new window on the request of user by clicking on the Image control. Is there any other way to catch it? – Muhammad Rizwan Aug 21 '13 at 12:44
  • I've yet to see large websites, retail mainly, that don't have their own image server to store a large amount of bitmaps. If it were possible i think people would have found a way around saving so many images by now... Just saying... – jcaruso Aug 21 '13 at 12:56
  • To uae a handler is a good solution, but I have my image in Bitmap form and I do not know to pass my Bimap variable as parameter to the Handler. If i pass it as session variable then error occurs "The Handler does not Implement interface member" :-( – Muhammad Rizwan Aug 22 '13 at 09:01
  • You need to not use the session and resize your bitmap before the iamge is requested, you need to resize the bitmap after the user clicks the link if you dont want to save it as a file. – John Boker Aug 22 '13 at 13:49