Solved

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

Posted on 2011-09-21
8
769 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SqlDataBase 7 46
ASP.NET MVC identity 6 26
Question about JQuery and asp.net 3 23
Get number of Files in Directory and Sub Directories 2 39
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

813 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

10 Experts available now in Live!

Get 1:1 Help Now