Solved

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

Posted on 2011-09-21
8
778 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IEnumerable<T> to a List<T> 8 51
orderby list (from Json) 1 27
Visual Studio 2015 auto inserted code 12 65
c#, case, if 4 16
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

685 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