Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-09-21
8
Medium Priority
?
801 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
[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
  • 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
Industry Leaders: 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 2000 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

Industry Leaders: 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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

705 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