Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-21
8
Medium Priority
?
811 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

877 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