lundi 23 février 2015

ASP.NET C# WebForms - How to display multiple frames from Image?

I created a generic image handler to render images to a webpage. The first code snippet works, it just does not render all the frames. It only renders the first one. I want to show all of the frames. In the example I am working with, I have a 14 frame image. How do I get all frames to return and render on my webpage? My second code snippet is my shot at it. I just need to get the rest of the way there. I referenced How to open a multi-frame TIFF imageformat image in .NET 2.0?.



public void processrequest(httpcontext context)
{
try
{
image img = null;
context.response.contenttype = "image/jpeg";

//get the imageid
string imageid = "";
if (context.session["imageid"] != null)
imageid = context.session["imageid"].tostring();
if (imageid != "")
{
//create your byte[] image
byte[] image;

//call service to return a image
image = a.downloadimage(imageid);

//store your image
memorystream ms = new memorystream(image, 0, image.length);
//get your image

img = image.fromstream(ms, true);
guid objguid = img.framedimensionslist[0];
framedimension dimension = new framedimension(objguid);

int height = 600;
int width = 900;
//get image frame count
int framecount = img.getframecount(dimension);

//resize the image
img = img.getthumbnailimage(width, height, new image.getthumbnailimageabort(thumbnailcallback), intptr.zero);

//save image -and output to render to screen
img.save(context.response.outputstream, imageformat.jpeg);
}
}
catch (exception ex)
{
string message = ex.tostring();
}
}


Now I want to take the above and change it to render all of the image frames.



public void ProcessRequest(HttpContext context)
{
try
{
Image img = null;
context.Response.ContentType = "image/Jpeg";

//Get the ImageId
string imageId = "";
if (context.Session["ImageId"] != null)
imageId = context.Session["ImageId"].ToString();
if (imageId != "")
{
//create your byte[] image
byte[] image;

//call service to return a image
image = a.DownloadImage(imageId);

for (int i = 0; i < 14; i++)
{
//store your image
MemoryStream ms = new MemoryStream(image, 0, image.Length);
//get your image

img = Image.FromStream(ms, true);
List<Image> images = new List<Image>();
Guid objGuid = img.FrameDimensionsList[0];
FrameDimension dimension = new FrameDimension(objGuid);
//Resize image
int height = 600;
int width = 900;
//Get image frame count
int frameCount = img.GetFrameCount(dimension);
img.SelectActiveFrame(FrameDimension.Page, i);
//resize the image
img = img.GetThumbnailImage(width, height, new Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);

//Save image -and output to render to screen
img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
images.Add(Image.FromStream(ms));
}
}
}
catch (Exception ex)
{
string message = ex.ToString();
}
}

Aucun commentaire:

Enregistrer un commentaire