简体   繁体   中英

printing but need image name

I am trying to print a single tiff file with multiple frames. However if I use javascript (window.print()) i get a print out of the entire webpage and not just the tiff image.

So I looked around StackOverflow and found some sample code. I was trying to implement it however the problem is the that the code works for an absolute image URL :- such as "C:\\img.jpeg"

I am wondering if anyone can show me how to transform my imgFax.ImageUrl to an actual image name? (otherwise I get an error:- "Illegal character in path" <--- in my System.Drawing.Image img = System.Drawing.Image.FromFile(imgFax.ImageUrl); code)

If anyone can show me some sample code that would be amazing! Thanks.

protected void PrintAll_Click(object sender, EventArgs e) 
{ 
  // number of frames 
  int number = _FaxPages.Count; 


 // for loop to iterate through each frame 
 for (int i = 0; i < number; i++) 
  { 
     // fax ID 
     string _FaxId = Page.Request["FaxId"]; 

     //string _Frame = Page.Request["Frame"]; 

     // current frame 
     _PageIndex = i; 

     // IMG URL 
     imgFax.ImageUrl = "ShowFax.ashx?n=" + _FaxId + "&f=" + _PageIndex + "&mw=750"; 



     PrintDocument pd = new PrintDocument(); 

     pd.PrintPage += PrintPage; 
     pd.Print();   

 }      

}

private void PrintPage(object o, PrintPageEventArgs e) 
{ 
   System.Drawing.Image img = System.Drawing.Image.FromFile(imgFax.ImageUrl); 
   Point loc = new Point(100, 100); 
   e.Graphics.DrawImage(img, loc); 
} 

I'm not sure whether you can at all do any printing in ASP via PrintDocument. This is all server-side code, while the printing is to be done at - and by the client's browser. I think that you will have to do it via JavaScript, but to not print the entire page - you will have to create another page that will only present the contents to be printed, then redirect the user to that smaller page (for example in a popup window) and then auto-print it via javascript. I;m not 100% sure, but all banking sites I use seem to follow that, and this is quite common in general..

For example, here's an article with this exact approach: http://www.dotnetcurry.com/ShowArticle.aspx?ID=92

just remember that your small 'print-page' should actually display the things you want to print :)

another nice link on printing images: http://forums.asp.net/post/3369436.aspx

You could split the single tiff image into separate files, as demonstrated here , then you could have a unique URL for each image:

static String[] SplitFile(String file_name)
{
    System.Drawing.Image imageFile = System.Drawing.Image.FromFile(file_name);
    System.Drawing.Imaging.FrameDimension frameDimensions = new System.Drawing.Imaging.FrameDimension(imageFile.FrameDimensionsList[0]);
    int NumberOfFrames = imageFile.GetFrameCount(frameDimensions);
    string[] paths = new string[NumberOfFrames];
    for (int intFrame = 0; intFrame < NumberOfFrames; ++intFrame)
    {
        imageFile.SelectActiveFrame(frameDimensions, intFrame);
        Bitmap bmp = new Bitmap(imageFile);
        paths[intFrame] = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + @"\" + intFrame.ToString() + ".tif";
        bmp.Save(paths[intFrame], System.Drawing.Imaging.ImageFormat.Tiff);
        bmp.Dispose();
    }
    imageFile.Dispose();
    return paths;
}

Eh, ok.. I know I already answered something, but I've noticed that you've marked the question bold.. So here it is: YOU CANNOT, there is NO WAY.

Url that you have that points to the image is just an internet link. It might point to the image, or to a website, or to some binary data. The remote webserver reads the URL parameters and decides what to do with it. There is no file name at all. Only URL of the webpage "ShowFax.ashx" and its parameters.

Some webpages may return you a special header, content-disposition/attachement, and in that header they may sometimes provide you with a filename. This is how "download" webpages provide the information to the browser so it may display a "Save file as.." window to the user. However, it probably is not the case. You are displaying the image in a aspx control that simply reads the image from an URL. It is all automated by the control, so you do not even have chance to peek the headers -- of course, unless you manually send the request, capture the stream, peek, redirect it back to the control, etc. IMHO, too much job for simple task of printing, and that would be only the beginning!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM