Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Displaying Base64 image in a user control

Posted on 2010-09-07
5
Medium Priority
?
703 Views
Last Modified: 2012-05-10
I have a Base64 encoded image stored in a session variable "EmrLabel" and I am trying to display the label-image in an .ascx control on an .aspx page.

The code below is bombing out on the line "Response.BinaryWrite(jpgStream.ToArray());" with the error: OutputStream is not available when a custom TextWriter is used.

How do I correct this?
using System;
using System.Net;
using System.Web;
using System.Xml;
using System.Data;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace MB.UserControl
{
    public partial class EmrLabel : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string merchandiseReturnLabel = Session["EmrLabel"].ToString();

            byte[] label = Convert.FromBase64String(merchandiseReturnLabel);
            System.Drawing.Image imgTiff = default(System.Drawing.Image);
            MemoryStream tiffStream = new MemoryStream();
            MemoryStream jpgStream = new MemoryStream();
            tiffStream.Write(label, 0, label.Length);
            imgTiff = System.Drawing.Image.FromStream(tiffStream);

            // Crop out crap.
            imgTiff = CropAndResizeImage(imgTiff);

            imgTiff.Save(jpgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            Response.Clear();
            Response.ContentType = "image/jpeg";
            Response.BinaryWrite(jpgStream.ToArray());
            Response.End();
        }

        private Image CropAndResizeImage(Image imgTiff)
        {
            // Crop image
            Rectangle cropArea = new Rectangle();
            cropArea.Width = 1520;
            cropArea.Height = 1030;
            cropArea.X = 50;
            cropArea.Y = 50;
            Bitmap bmpImage = new Bitmap(imgTiff);
            Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);

            return (Image)bmpCrop;
        }
    }
}

Open in new window

0
Comment
Question by:mandi224
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 33620583
When you output image data, that has to be data for the WHOLE URL, not just a control on the URL - when you manipulate the "Response" object, it is the whole page that gets cleared.

When you program at the level of an individual CONTROL, you shouldn't be clearing the whole page.  Just create an image with a specific URL, and then the Page_Load event for that entire page (URL) can do the same thing that you are currently trying to do for the Page_Load of a single control.
0
 

Author Comment

by:mandi224
ID: 33620784
jensfiederer - thanks for your help. I hope I'm understanding correctly.

The way I had originally designed this, I had a page "ShippingPackingSlip.aspx" and on that page, I had

Where the EmrLabel.aspx page had the code above that I was attempting to put into a user control.

Problem was, when I open EmrLabel.aspx that way, Session["EmrLabel"] is null. (Even though it's NOT null in the codebehind on ShippingPackingSlip.aspx.

I also tried passing it as a querystring -- like EmrLabel.aspx?label=[Base64 Value] (which I didn't want to do) but it's too long (apparently?) and doesn't work either.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 33620907
The original design sounds like the right way.

You are right, passing a query string with the Base64 value is much too long....a string with just enough info to retrieve (or generate) the value would be better....

But I gather that is what you wanted to do with Session["EmrLabel"] (presumably the contents are the Base64 string as in your current code).....I really don't see why Session["EmrLabel"] should be null.  Where and how is Session["EmrLabel"] set to a value?

0
 

Author Comment

by:mandi224
ID: 33621274
Session["EmrLabel"] is the Base64 string, yes.

The user goes to ShippingMaterials.aspx -- clicks the order for the page they want to generate labels for.
It is on THIS page that I'm generating the label (so I can easily catch errors before actually printing the label) So I generate the label and add it to the session ... Session.Add("EmrLabel", merchandiseReturnLabel);

On that page, I check
If (Session["EmrLabel"] != null)

Then I go ahead and display ShippingPackingSlip.aspx which includes the
 

When I debug, Session["EmrLabel"] is not null on ShippingMaterials.aspx, it is not null on ShippingPackingSlip.aspx, but it is null on EmrLabel.aspx
0
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 2000 total points
ID: 33621684
This should work.  Is it possible you are doing something to interfere with the session (not allowing cookies, for example, and forcing the session id to be stored in the URL, which in your scenario does not get generated for the other page)?

The attached sample uses a similar scheme (ImagePrep just stores a user-entered value in the session and redirects to the ImageContainer, which contains ImageActual - which just draws the user-entered value on an image and displays it)


ImageActual.aspx
ImageActual.aspx.cs
ImageContainer.aspx
ImagePrep.aspx
ImagePrep.aspx.cs
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question