===============GDI+======================

zl194 2009-12-28 03:31:12
下面的代码是我写的一个类似于股票图的代码,大意是在一个底稿上画2条曲线,一个标准值,一个检测值,并画出x和y的坐标轴刻度和图例。

方法可以正常运行,也能得到图像,在win2003的机器上,总是在程序运行很久以后(如这个方法运行结束了几分钟后)提示GDI+错误,在Winxp上就很少有提示,至少我测试的机器上没出过。

public static Bitmap writeImage(Bitmap image,double[] dubleY1, double[] DubleY2, int[] xLine1,int[] xLine2, double yStart, double yUnit, int xUnit, string yName,int tt,int xStart)
{
Pen p1 = Pens.Blue; //写图例的画笔
Pen p2 = Pens.Red;
Brush bs1 = Brushes.Blue; //画线用的
Brush bs2 = Brushes.Red;
int[] y1 = getYpoint1(dubleY1, yStart, yUnit); //Y轴的坐标点
int[] y2 = getYpoint1(DubleY2, yStart, yUnit); //Y轴的坐标点

//Bitmap image = Resource2.KT3;//线图底稿

Bitmap newimage = new Bitmap(image.Width, image.Height);
Graphics oGraphic = Graphics.FromImage(newimage);
Point p00 = new Point(0, 0);
oGraphic.DrawImage(image, p00);
image.Dispose();
image = newimage;


Point[] py1 = new Point[y1.Length];
Point[] py2 = new Point[y2.Length];

……这里有一段得到Y 点数组的代码,取得每个Y点的位置

using (Graphics g = Graphics.FromImage(image)) //在背景图上画 折线图
{
if (py1.Length > 1)
{
g.DrawLines(p1, py1);//标准值 曲线
}
if (py2.Length > 1)
{
g.DrawLines(p2, py2);//检测值 曲线
}

//画y轴坐标
for (int i = 0; i <7; i++)
{
double iYvalue = yStart + yUnit * 20 * i;
g.DrawString(iYvalue.ToString("0.00"), ft, bs2, theYs[i]);

}
//画x轴坐标
for (int i = 0; i <5; i++)
{
string xValue = "0";

int theXvalue = (i * xUnit + 1) * 50;
xValue = Convert.ToInt32(i * 5 * xUnit+xStart).ToString();


g.DrawString(xValue, ft, Brushes.White, theXs[i]);
}
//画出图例
int leng = yName.Length;
Point pA;
Point pB;
switch (tt)
{
case 0:
pA = new Point(700, 20);
pB = new Point(700, 35);
^ default:
pA = new Point(1820, 1160);
pB = new Point(1820, 1175);
break;
}

g.DrawString("[" + yName + ":标准值]", ft2, bs1, pA);
g.DrawString("[" + yName + ":检测值]", ft2, bs2, pB);


}
return image;


}


谁能帮我分析分析是怎么回事呢,或者这种不是实时出现的错误怎么测试呢。
...全文
327 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
LorenLiu 2009-12-31
  • 打赏
  • 举报
回复
把image.Dispose();
image = newimage;
去掉,然后后面的image的地方都改用newimage试试

不太明白为什么要dispose掉image,然后把newimage赋给image再对image操作。。。
wo6522317 2009-12-31
  • 打赏
  • 举报
回复
这个 image 对象返回后有保存操作吗?
lovelan1748 2009-12-31
  • 打赏
  • 举报
回复
帮顶
qiqundelang 2009-12-31
  • 打赏
  • 举报
回复
可以!
huipengzheng@sina.com
zl194 2009-12-31
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 qiqundelang 的回复:]
要是愿意代码发给我
我给你调!
[/Quote]

谢谢!光给你一个图像类行吗,别的部分我不能做主,如过你有win2003的环境就帮我测测。
zl194 2009-12-31
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 yuxuanji 的回复:]
Bitmap newimage = new Bitmap(image.Width, image.Height);
Graphics oGraphic = Graphics.FromImage(newimage);
Point p00 = new Point(0, 0);
oGraphic.DrawImage(image, p00);
image.Dispose();


这段代码的逻辑混乱,究竟是要干什么?
你可以用一个 Rectangle
再用Graphics.DrawPie
[/Quote]

这段是有些问题,原想是释放掉image,在新的图像里操作,避免原图像的锁定。现在改了,将image = newimage;去掉了,后面的引用全部使用newimage,并返回newimage.但还是不行。
zl194 2009-12-31
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 lorenliu 的回复:]
把image.Dispose();
  image = newimage;
去掉,然后后面的image的地方都改用newimage试试

不太明白为什么要dispose掉image,然后把newimage赋给image再对image操作。。。
[/Quote]

这段我也检查有问题,现在把 image=newimage;去掉了,直接使用newimage,最后返回newimage.
但还是有同样的错误。我现在开始感觉是win2003的问题了。愁!!
qiqundelang 2009-12-31
  • 打赏
  • 举报
回复
要是愿意代码发给我
我给你调!
LutzMark 2009-12-31
  • 打赏
  • 举报
回复
Bitmap newimage = new Bitmap(image.Width, image.Height);
Graphics oGraphic = Graphics.FromImage(newimage);
Point p00 = new Point(0, 0);
oGraphic.DrawImage(image, p00);
image.Dispose();
image = newimage;

这段代码的逻辑混乱,究竟是要干什么?
你可以用一个 Rectangle
再用Graphics.DrawPie
zl194 2009-12-30
  • 打赏
  • 举报
回复
还是不行啊。有没有什么方法能测试出到底是什么位置出的错误。每次错误都是在没有任何操作的情况下弹出的。
wzuomin 2009-12-28
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 wuyq11 的回复:]
使用了相同的文件名称,或者上次的对象没有Dispose掉 
解决A generic error occurred in GDI+
[/Quote]

帮顶
wuyq11 2009-12-28
  • 打赏
  • 举报
回复
使用了相同的文件名称,或者上次的对象没有Dispose掉
解决A generic error occurred in GDI+
特别 2009-12-28
  • 打赏
  • 举报
回复
没有server的OS
不好测
tianliang1 2009-12-28
  • 打赏
  • 举报
回复
gdi+ 后生来看哈。。。帮顶。。。
fut20090715 2009-12-28
  • 打赏
  • 举报
回复
会不会跟垃圾回收有关
因为server和workstation的机制不一样
ourola 2009-12-28
  • 打赏
  • 举报
回复
GDI+ 的一般性错误. 你去查查资料就知道了..
adrianEvin 2009-12-28
  • 打赏
  • 举报
回复
定下
zl194 2009-12-28
  • 打赏
  • 举报
回复
等待中
zl194 2009-12-28
  • 打赏
  • 举报
回复
错误是:
对话框标题:Microsoft .NET Framework
内容:
Unhandled exception has occured in your application If
you click Continue,the application will ignore this
error and attemp to contion.If you click Quit,
the application will close immediately

A generic error occurred in GDI+

详细:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
at System.Drawing.Graphics.Clear(Color color)
at System.Windows.Forms.ToolStripSystemRenderer.RenderStatusStripBackground(ToolStripRenderEventArgs e)
at System.Windows.Forms.ToolStripSystemRenderer.OnRenderToolStripBackground(ToolStripRenderEventArgs e)
at System.Windows.Forms.ToolStripRenderer.DrawToolStripBackground(ToolStripRenderEventArgs e)
at System.Windows.Forms.ToolStrip.OnPaintBackground(PaintEventArgs e)
at System.Windows.Forms.StatusStrip.OnPaintBackground(PaintEventArgs e)
at System.Windows.Forms.Control.PaintTransparentBackground(PaintEventArgs e, Rectangle rectangle, Region transparentRegion)
at System.Windows.Forms.Control.PaintTransparentBackground(PaintEventArgs e, Rectangle rectangle)
at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backColor, Point scrollOffset)
at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle)
at System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.StatusStrip.RightToLeftLayoutGrip.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
dmProduction
Assembly Version: 1.0.0.2
Win32 Version: 1.0.0.2
CodeBase: file:///D:/Program%20Files/XXXX系统/dmProduction.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
ClassMyDate
Assembly Version: 1.0.0.4
Win32 Version: 1.0.0.4
CodeBase: file:///D:/Program%20Files/XXXX系统/ClassMyDate.DLL
----------------------------------------
System.Data
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Transactions
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Transactions/2.0.0.0__b77a5c561934e089/System.Transactions.dll
----------------------------------------
System.EnterpriseServices
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.1433 (REDBITS.050727-1400)
CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.EnterpriseServices/2.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
----------------------------------------
cplan
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///D:/Program%20Files/XXXX系统/cplan.DLL
----------------------------------------
taskprocess
Assembly Version: 1.0.0.3
Win32 Version: 1.0.0.3
CodeBase: file:///D:/Program%20Files/XXX系统/taskprocess.DLL
----------------------------------------
zgraph
Assembly Version: 1.0.0.1
Win32 Version: 1.0.0.1
CodeBase: file:///D:/Program%20Files/XXXX系统/zgraph.DLL
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.


zl194 2009-12-28
  • 打赏
  • 举报
回复
等我去超一下,错误原文。
加载更多回复(13)

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧