Solved

Save WebControls.Image to a file or to system.drawings.image

Posted on 2011-09-21
8
752 Views
Last Modified: 2012-05-12
I am generating USPS barcode images so they can be printed to an address label and shipped. I purchased an ASP.Net USPS barcode image creator so It creates the image in an ASP WebControls.Image control. But when its time to print, the only thing i can send to the printer from C# is a system.drawings.image object:

//Get the default printer name.
                var prnDocument = new PrintDocument { PrinterSettings = { PrinterName = printer } };
                              prnDocument.PrintPage += PrintOrderLabels;
                prnDocument.Print();


  private void PrintOrderLabels(object sender, PrintPageEventArgs e)
        {
      var fnt = new Font(FontFamily.GenericSerif, 14);
 float yPos = 1;
            float xPos = 15;
            e.Graphics.DrawString("Big D, Tx", fnt, Brushes.Black, 1, yPos);
            e.Graphics.DrawImage(barcode,xPos,yPos); //barcode is a webcontrols.image so this wont compile
}

The  e.Graphics.DrawImage function only accepts an object of type system.drawings.image. So how can I:

A. convert a webcontrols.image control to a system.drawings.image object? (i dont think you can unless the image is physically on the hard drive)

B. Save the webcontrols.image object to a file on the server and retrieve it from there

or

C.  send the web image to the printer along with my other string data?


Thanks.
0
Comment
Question by:troycomp
  • 4
  • 4
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36577981
Are you saying that you have a web site with an <asp:Image> control, and you need to download the image from the web site, and print it using a separate application?  That sounds like a job for the System.Net.WebClient or HttpWebRequest to get the byte array, and convert it to an image.
0
 

Author Comment

by:troycomp
ID: 36578266
yes i have a site with an <asp:image> control...but the same site has a print label button which prints to a local printer, this is an intranet site by the way. I can print text no problem, but the barcode is an <asp:image>. how do I print that like i outlined above?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36580525
If this is all happening within the same web site, what is the source for the Image control?  If it is a file, then you might be able to use Image.FromFile(Server.MapPath(path)) to read the file into a System.Drawing.Image structure.
0
 

Author Comment

by:troycomp
ID: 36580948
The image is generated from a 3rd party app. It is never ever stored on my server. Its dynamically created and then returned in an <asp:image>  with an ImageUrl of "linear.aspx?data=38&type=50".  go to:
http://troycomptonworld.com/barcode/LinearWebStreamDemo.aspx for an example. The image at the top of the page is what im trying to capture so i can print it to an address label. Its being created with this call in linear.aspx:

<%@ Page Language="C#" %>
<%@ Import Namespace="BarcodeLib.Barcode.Linear" %>
<%
      LinearWebStream.drawBarcode(Request, Response);
   
%>
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 96

Expert Comment

by:Bob Learned
ID: 36581127
If you have a URL to the image, then you should be able to use the WebClient to download the byte array, to construct an image, or download the file.

Download Files from Web [C#]
http://www.csharp-examples.net/download-files/

WebClient webClient = new WebClient();
webClient.DownloadFile("http://mysite.com/myfile.txt", @"c:\myfile.txt");

Open in new window



0
 

Author Comment

by:troycomp
ID: 36581703
I cant do it that way. I get "Illegal characters in path" :

 barcodeImage.ImageUrl = "linear.aspx?Data=75215187103&Type=38";
            var webClient = new WebClient();
            webClient.DownloadFile(barcodeImage.ImageUrl, @"c:\myfile.png");
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 36581719
It needs to be a fully-qualified URL, with http://, so it can figure out the protocol.
0
 

Author Comment

by:troycomp
ID: 36581754
Bless you. That worked like a charm
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now