CreateDC在203rd循环上抛出错误。 [英] CreateDC throws error on 203rd loop.

查看:90
本文介绍了CreateDC在203rd循环上抛出错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在写一些互操作代码将图形打印到eps文件

我使用adobe打印机驱动程序。我写的代码是

正常工作。但如果我循环超过200次,则会产生

错误。任何人都可以告诉我它为什么会抛出错误或者我有记忆

问题?我通过它进行了调试,我发现createDC在203rd循环中返回0

并抛出异常。为什么createDC返回0?

这是执行互操作的代码段。


使用System;

using System.Drawing;

使用System.IO;

使用System.Drawing.Printing;

使用System.Runtime.InteropServices;

使用System.Drawing.Text;


命名空间打印机

{

公共类CPrinter

{

私人图形pDC;

私人IntPtr dc1;

私人IntPtr hdevmode;


[StructLayout(LayoutKind.Sequential)]

public struct DOCINFO

{

[MarshalAs(UnmanagedType.LPWStr)] public string pDocName ;

[MarshalAs(UnmanagedType.LPWStr)] public string pOutputFile;

[MarshalAs(UnmanagedType.LPWStr)] public string pDataType;

}


[DllImport(

" winspool.drv",CharSet = CharSet.Unicode,ExactSpelli ng = false,

CallingConvention = CallingConvention.StdCall )]

public static extern long StartDocPrinter(IntPtr hPrinter,int Level,

ref DOCINFO pDocInfo);


[DllImport( " gdi32.dll",EntryPoint =" CreateDCA")]

public static extern IntPtr CreateDC(string lpDriverName,string

lpDeviceName,

string lpOutput,IntPtr pDevMode);


[DllImport(" gdi32.dll")]

static extern bool DeleteDC(IntPtr hdc);


[DllImport(" gdi32",EntryPoint =" StartDocA")]

public static extern int StartDoc(IntPtr dc1,ref DOCINFO di);


[DllImport(" gdi32")] public static extern int StartPage(IntPtr hDC);

[DllImport(" gdi32")] public static extern int EndDoc(IntPtr hDC);


[DllImport(" gdi32")] public static extern int EndPage(IntPtr hDC);


public CPrinter()

{

}

public void StartPrinter(string ou tputFile)

{

试试

{

DOCINFO di =新DOCINFO();

di.pOutputFile = outputFile;

PrintDocument pd = new PrintDocument();

hdevmode = pd.PrinterSettings.GetHdevmode();

dc1 = CreateDC(""," Acrobat Distiller 3x2",outputFile,hdevmode);

int result = StartDoc(dc1,ref di);

结果= StartPage(dc1);

pDC = Graphics.FromHdc(dc1);

pDC.PageUnit = GraphicsUnit.Pixel;

}

catch(Exception ex)

{

throw(ex);

}

}


public void EndPrinter()

{

int result = EndPage(dc1);

result = EndDoc(dc1);

DeleteDC(dc1);

}

}

}


这是我循环的代码段:


使用系统;

使用打印机;


命名空间ConsoleApplication1

{

class Class1

{

[STAThread]

static void Main(string [] args)

{

int i = 0;

尝试

{

而(true)

{

CPrinter Adob​​ePrinter = new CPrinter();

Adob​​ePrinter.StartPrinter(" temp.wrk");

Adob​​ePrinter.EndPrinter();

i ++;

Console.WriteLine(i);

}

}

catch(例外情况ex)

{

Console.WriteLine(i +" : + ex);

}

}

}

}


谢谢,

Lalasa。

Hi guys,

I am writing some interop code to print graphics to an eps file
and I use adobe printer driver. The piece of code I have written is
working fine. but if I loop for more than 200 times, it is throwing an
error. Can anybody tell me why it throws an error or am I having memory
problems? I debuged through it and I see that createDC is returning 0
on 203rd loop and throws an exception. Why is createDC returning a 0?
This is the piece of code that does the interop.

using System;
using System.Drawing;
using System.IO;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Drawing.Text;

namespace Printer
{
public class CPrinter
{
private Graphics pDC;
private IntPtr dc1;
private IntPtr hdevmode;

[StructLayout( LayoutKind.Sequential)]
public struct DOCINFO
{
[MarshalAs(UnmanagedType.LPWStr)]public string pDocName;
[MarshalAs(UnmanagedType.LPWStr)]public string pOutputFile;
[MarshalAs(UnmanagedType.LPWStr)]public string pDataType;
}

[DllImport(
"winspool.drv",CharSet=CharSet.Unicode,ExactSpelli ng=false,
CallingConvention=CallingConvention.StdCall )]
public static extern long StartDocPrinter(IntPtr hPrinter, int Level,
ref DOCINFO pDocInfo);

[DllImport("gdi32.dll", EntryPoint="CreateDCA")]
public static extern IntPtr CreateDC(string lpDriverName, string
lpDeviceName,
string lpOutput, IntPtr pDevMode);

[DllImport("gdi32.dll")]
static extern bool DeleteDC(IntPtr hdc);

[DllImport("gdi32", EntryPoint="StartDocA")]
public static extern int StartDoc(IntPtr dc1, ref DOCINFO di);

[DllImport("gdi32")] public static extern int StartPage(IntPtr hDC);
[DllImport("gdi32")] public static extern int EndDoc(IntPtr hDC);

[DllImport("gdi32")] public static extern int EndPage(IntPtr hDC);

public CPrinter()
{
}
public void StartPrinter(string outputFile)
{
try
{
DOCINFO di = new DOCINFO();
di.pOutputFile = outputFile;
PrintDocument pd = new PrintDocument();
hdevmode = pd.PrinterSettings.GetHdevmode();
dc1 = CreateDC("","Acrobat Distiller 3x2",outputFile,hdevmode);
int result = StartDoc(dc1, ref di);
result = StartPage(dc1);
pDC = Graphics.FromHdc(dc1);
pDC.PageUnit = GraphicsUnit.Pixel;
}
catch(Exception ex)
{
throw(ex);
}
}

public void EndPrinter()
{
int result = EndPage(dc1);
result = EndDoc(dc1);
DeleteDC(dc1);
}
}
}

This is the piece of code where I loop :

using System;
using Printer;

namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int i = 0;
try
{
while (true)
{
CPrinter AdobePrinter = new CPrinter();
AdobePrinter.StartPrinter("temp.wrk");
AdobePrinter.EndPrinter();
i++;
Console.WriteLine(i);
}
}
catch (Exception ex)
{
Console.WriteLine( i + " : " + ex);
}
}
}
}

Thanks,
Lalasa.

推荐答案

" L" < LA ******** @ investors.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...
"L" <la********@investors.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
大家好,

我正在写一些互操作代码将图形打印到eps文件
我使用adobe打印机驱动程序。我写的代码片段工作正常。但如果我循环超过200次,则会出现
错误。任何人都能告诉我它为什么会抛出错误或者我有记忆问题吗?我通过它进行了调试,我发现createDC在203rd循环中返回0
并抛出异常。为什么createDC返回0?

您正在使用Disposable对象(Graphics)而不进行处理。


这是一段代码互操作。

使用System;
使用System.Drawing;
使用System.IO;
使用System.Drawing.Printing;
使用System.Runtime.InteropServices;
使用System.Drawing.Text;

名称空间打印机
公共类CPrinter
{
私有图形pDC;
私人IntPtr dc1;
私人IntPtr hdevmode;

[StructLayout(LayoutKind.Sequential)]
公共结构DOCINFO
{
[MarshalAs(UnmanagedType.LPWStr)] public string pDocName;
[MarshalAs(UnmanagedType.LPWStr)] public string pOutputFile;
[MarshalAs(UnmanagedType.LPWStr)] public string pDataType;
}

[DllImport(
" winspool.drv",CharSet = CharSet.Unicode,ExactSpelli ng = false,
CallingConvention = CallingConvention.StdCall)]
公共静态extern long StartDocPrinter(IntPtr hPrinter,int Level,
ref DOCINFO pDocInfo);

[DllImport(" gdi32.dll",EntryPoint =" CreateDCA")]
public static extern IntPtr CreateDC(string lpDriverName,string
lpDeviceName,
string lpOutput,IntPtr pDevMode);

[DllImport(" gdi32.dll")]
static extern bool DeleteDC(IntPtr hdc);

[DllImport(" gdi32",EntryPoint =" StartDocA")]
public static extern int StartDoc(IntPtr dc1,ref DOCINFO di);

[DllImport(" gdi32")] public static extern int StartPage(IntPtr hDC);

[DllImport(" gdi32")] public static extern int EndDoc( IntPtr hDC);

[DllImport(" gdi32")] public static extern int EndPage(IntPtr hDC);

公共CPrinter()
{
}
public void StartPrinter(string outputFile)
{
尝试
{DOCINFO di = new DOCINFO();
di.pOutputFile = outputFile ;
PrintDocument pd = new PrintDocument();
hdevmode = pd.PrinterSettings.GetHdevmode();
dc1 = CreateDC(""," Acrobat Distiller 3x2",outputFile,hdevmode);
int result = StartDoc(dc1,ref di);
result = StartPage(dc1);
pDC = Graphics.FromHdc(dc1);
pDC.PageUnit = GraphicsUnit.Pixel;
}
catch(Exception ex)
{
throw(ex);
}


public void EndPrinter( )
{
int result = EndPage(dc1);
result = EndDoc(dc1);
DeleteDC(dc1);
}
}
使用System;
使用Printer;

命名空间ConsoleApplication1
{class class
{STAThread]
static void Main(string [] args)
{
int i = 0; <尝试
{
while(true)
{
CPrinter Adob​​ePrinter = new CPrinter();
Adob​​ePrinter.StartPrinter(" temp.wrk");
Adob​​ePrinter.EndPrinte r();
i ++;
Console.WriteLine(i);
}
}
catch(Exception ex)
{
控制台.WriteLine(i +" : + ex);
}
}
}

谢谢,Lalasa。
Hi guys,

I am writing some interop code to print graphics to an eps file
and I use adobe printer driver. The piece of code I have written is
working fine. but if I loop for more than 200 times, it is throwing an
error. Can anybody tell me why it throws an error or am I having memory
problems? I debuged through it and I see that createDC is returning 0
on 203rd loop and throws an exception. Why is createDC returning a 0?

You are using a Disposable object (Graphics) without disposing it.


This is the piece of code that does the interop.

using System;
using System.Drawing;
using System.IO;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Drawing.Text;

namespace Printer
{
public class CPrinter
{
private Graphics pDC;
private IntPtr dc1;
private IntPtr hdevmode;

[StructLayout( LayoutKind.Sequential)]
public struct DOCINFO
{
[MarshalAs(UnmanagedType.LPWStr)]public string pDocName;
[MarshalAs(UnmanagedType.LPWStr)]public string pOutputFile;
[MarshalAs(UnmanagedType.LPWStr)]public string pDataType;
}

[DllImport(
"winspool.drv",CharSet=CharSet.Unicode,ExactSpelli ng=false,
CallingConvention=CallingConvention.StdCall )]
public static extern long StartDocPrinter(IntPtr hPrinter, int Level,
ref DOCINFO pDocInfo);

[DllImport("gdi32.dll", EntryPoint="CreateDCA")]
public static extern IntPtr CreateDC(string lpDriverName, string
lpDeviceName,
string lpOutput, IntPtr pDevMode);

[DllImport("gdi32.dll")]
static extern bool DeleteDC(IntPtr hdc);

[DllImport("gdi32", EntryPoint="StartDocA")]
public static extern int StartDoc(IntPtr dc1, ref DOCINFO di);

[DllImport("gdi32")] public static extern int StartPage(IntPtr hDC);
[DllImport("gdi32")] public static extern int EndDoc(IntPtr hDC);

[DllImport("gdi32")] public static extern int EndPage(IntPtr hDC);

public CPrinter()
{
}
public void StartPrinter(string outputFile)
{
try
{
DOCINFO di = new DOCINFO();
di.pOutputFile = outputFile;
PrintDocument pd = new PrintDocument();
hdevmode = pd.PrinterSettings.GetHdevmode();
dc1 = CreateDC("","Acrobat Distiller 3x2",outputFile,hdevmode);
int result = StartDoc(dc1, ref di);
result = StartPage(dc1);
pDC = Graphics.FromHdc(dc1);
pDC.PageUnit = GraphicsUnit.Pixel;
}
catch(Exception ex)
{
throw(ex);
}
}

public void EndPrinter()
{
int result = EndPage(dc1);
result = EndDoc(dc1);
DeleteDC(dc1);
}
}
}

This is the piece of code where I loop :

using System;
using Printer;

namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int i = 0;
try
{
while (true)
{
CPrinter AdobePrinter = new CPrinter();
AdobePrinter.StartPrinter("temp.wrk");
AdobePrinter.EndPrinter();
i++;
Console.WriteLine(i);
}
}
catch (Exception ex)
{
Console.WriteLine( i + " : " + ex);
}
}
}
}

Thanks,
Lalasa.





我将pDC.Dispose()添加到我的EndPrinter()函数中,但仍然是我有问题的b $ b。


public void EndPrinter()

{

int result = EndPage(dc1);

result = EndDoc (dc1);

pDC.Dispose();

DeleteDC(dc1);

}


谢谢,

L.


I added the pDC.Dispose() to my EndPrinter() function and still I am
having the problem.

public void EndPrinter()
{
int result = EndPage(dc1);
result = EndDoc(dc1);
pDC.Dispose();
DeleteDC(dc1);
}

Thanks,
L.


有人可以告诉我它为什么会抛出错误还是我有记忆问题?


你正在释放GetHDevmode返回的句柄。


public static extern long StartDocPrinter(IntPtr hPrinter,int Level,
Can anybody tell me why it throws an error or am I having memory
problems?
You''re net freeing the handle returned by GetHDevmode.

public static extern long StartDocPrinter(IntPtr hPrinter, int Level,



^^^^


返回类型应为(u)int。


Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请只回复新闻组。


^^^^

The return type should be (u)int.

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


这篇关于CreateDC在203rd循环上抛出错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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