gdi + c#中发生一般错误 [英] generic error occurred in gdi+ c#

查看:86
本文介绍了gdi + c#中发生一般错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我
我尝试从Windows应用程序进行打印.运行该应用程序时,我按ctrl + P进行打印,然后显示打印对话框,这时我尝试将此内容保存到pdf转换器中.然后显示一条错误消息"gdi +中发生一般错误".这些类型错误发生的原因是什么.如何克服这个错误?


我的代码在下面给出


please help me
I try to take a print from windows application . when run the application ,I Press ctrl+P for take print, then print dialog box display, at this time I try to save this content into pdf convertor. then diaplay an error message " generic error occurred in gdi+". what is the reasin for occure these type error . How to over come this error?


my code is given below


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;
using System.Net;

namespace data
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection("server=.;database=billing;uid=sa;pwd=sqladmin");
        private System.IO.Stream streamToPrint;
        string streamType;

        public Form1()
        {
            InitializeComponent();
           


        }
        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private static extern bool BitBlt
        (
            IntPtr hdcDest, // handle to destination DC
            int nXDest, // x-coord of destination upper-left corner
            int nYDest, // y-coord of destination upper-left corner
            int nWidth, // width of destination rectangle
            int nHeight, // height of destination rectangle
            IntPtr hdcSrc, // handle to source DC
            int nXSrc, // x-coordinate of source upper-left corner
            int nYSrc, // y-coordinate of source upper-left corner
            System.Int32 dwRop // raster operation code
        );
        private void Form1_Load(object sender, EventArgs e)
        {
            SqlDataAdapter da = new SqlDataAdapter("select itemcode,itemname from bill", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];

           
        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
            int x = e.MarginBounds.X;
            int y = e.MarginBounds.Y;
            int width = 480;
            int height = Int16.MaxValue ;
            if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
            {
                width = e.MarginBounds.Width;
                height = image.Height * e.MarginBounds.Width / image.Width;
            }
            else
            {
                height = e.MarginBounds.Height;
                width = image.Width * e.MarginBounds.Height / image.Height;
            }
            System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
            e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel  );
        }
        public void StartPrint(Stream streamToPrint, string streamType)
        {
            this.printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
            this.streamToPrint = streamToPrint;
            this.streamType = streamType;
            System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();
            PrintDialog1.AllowSomePages = true;
            PrintDialog1.ShowHelp = true;
            PrintDialog1.Document = printDocument1;

            DialogResult result = PrintDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                printDocument1.Print();
                //docToPrint.Print();
            }

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.P)
            {
                Graphics g1 = this.CreateGraphics();
                Image MyImage = new Bitmap(this.panel1.Width, this.panel1.Height, g1);
                Graphics g2 = Graphics.FromImage(MyImage);
                IntPtr dc1 = g1.GetHdc();
                IntPtr dc2 = g2.GetHdc();
                BitBlt(dc2, 0, 0, this.panel1.Width, this.panel1.Height, dc1, 0, 0, 13369376);
                g1.ReleaseHdc(dc1);
                g2.ReleaseHdc(dc2);
                MyImage.Save(@"c:\PrintPage.jpg", ImageFormat.Jpeg);
                FileStream fileStream = new FileStream(@"c:\PrintPage.jpg", FileMode.Open, FileAccess.Read);
                StartPrint(fileStream, "Image");
                fileStream.Close();

                if (System.IO.File.Exists(@"c:\PrintPage.jpg"))
                {
                    System.IO.File.Delete(@"c:\PrintPage.jpg");
                }
            }
        }
        }
    }

推荐答案

如果在保存数据的行上引发了错误,请
If the error is thrown on the line where you are saving your data, this post[^] might help.


这篇关于gdi + c#中发生一般错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆