打印图像数组

时间:2013-11-14 20:02:20

标签: c# image list printing

我继承了一个需要打印存储在数据库中的图像数组的应用程序。我编写了代码来通过Web服务检索图像,并将图像放入BitmapImage数组中。

然而,当我将BitmapImage添加到Image的源时,图像则为空。没有抛出任何异常,但似乎没有图像添加到列表中,只要我将图像添加到数组中,代码就会暂停。

任何提示都将不胜感激。

在课程标题中:

\\BitmapImage is System.Windows.Media.Imaging.BitmapImage

public List<BitmapImage> prtImages;


完成异步获取图像时Web服务的事件处理程序:

void client_GetImagesCompleted(object sender, OrderImageWebService.GetOrderImagesCompletedEventArgs e)
{
    if (e.Error != null)
    {
        System.Windows.Browser.HtmlPage.Window.Eval("alert('Error occurred loading images.');");
        throw new Exception(string.Format("Error loading image edits: {0}", e.Error));
    }
    else if ((!e.Cancelled) && (e.Result != null))
    {
        try
        {
            for (int i = 0; i <= imgs; i++)
            {
                byte[] bImage = loi[i].Content;
                using (System.IO.MemoryStream sioms = 
                    new System.IO.MemoryStream(bImage, 0, bImage.Length))
                {
                    bi.SetSource(sioms);
                    int ph = bi.PixelHeight;  //Alert shows 3296
                    int pw = bi.PixelWidth;   //Alert shows 2560
                    System.Windows.Browser.HtmlPage.Window.Eval(
                        string.Format("alert('BI image is {0} by {1}.');", 
                        ph.ToString(), pw.ToString()));

                        prtImages.Add(bi);

                        //This alert never shows
                        System.Windows.Browser.HtmlPage.Window.Eval(
                            string.Format("alert('prtImages has size {1} after add.');",  
                            prtImages.Count.ToString()));
                }
            }
        }
        catch(Exception ex)
        {
            //This exception is never caught
            System.Windows.Browser.HtmlPage.Window.Eval(string.Format("alert('{0}');", ex.Message));
        }
    }
    else
    {
        System.Windows.Browser.HtmlPage.Window.Eval("alert('Oops. Error.');");
    }

    System.Windows.Browser.HtmlPage.Window.Eval("alert('Clearing wait.');");

    bPrtImageLoaded = true;
    ewh_gotImages.Set();
}



在GetPrintImage中:

public Image GetPrintImage(int iPageID)
{
    if (bPrtImageLoaded)
    {
        Image img = new Image();
        img.Source = prtImages[iPageID];

        double ah = img.ActualHeight;
        double aw = img.ActualWidth;

        System.Windows.Browser.HtmlPage.Window.Eval(
            string.Format("alert('Image is {0} by {1}.');", 
            ah.ToString(), aw.ToString()));

        if (null != img)
        {
            return (img);
        }
    }

    return(null);
}



从按钮单击调用

private void p_printMultiplePages(object sender, System.Windows.Printing.PrintPageEventArgs e)
{
    Image img = GetPrintImage(iCurPrintPage);

    if (null != img)
    {
        if (e.PrintableArea.Height < img.ActualHeight)
        {
            scale = e.PrintableArea.Height / img.ActualHeight;
        }

        if ((e.PrintableArea.Width < img.ActualWidth) && ((e.PrintableArea.Width / img.ActualWidth) < scale))
        {
            scale = e.PrintableArea.Width / img.ActualWidth;
        }

        if (scale < 1)
        {
            scaleTransform.ScaleX = scale;
            scaleTransform.ScaleY = scale;
            img.RenderTransform = scaleTransform;
        }

        e.PageVisual = img;

        iCurPrintPage++;
        if (iCurPrintPage < SessionState.PageCount)
        {
            e.HasMorePages = true;
        }
        else
        {
            e.HasMorePages = false;
        }
    }
    else
    {
        System.Windows.Browser.HtmlPage.Window.Eval(
            string.Format("alert('Image {0} of order {1} is null.');", 
            iCurPrintPage.ToString(), SessionState.OrderID.ToString()));
        e.HasMorePages = false;
    }
}

感谢您提供的任何提示,

布鲁斯。

1 个答案:

答案 0 :(得分:0)

我建议从Web服务获取图像,因为二进制数据(byte [])将每个图像编码为base64并发布base64字符串数组。在客户端上,您将解码数组的每个元素,其中base64 stings返回byte []。使用MemoryStream读取它并将该对象传递给图像阅读器(阅读本文reading image from bytes

但在你做任何事情之前,请确保你的p_printMultiplePages方法可以显示本地图像