我可能搞砸了一些东西,现在不知道如何修复它。
有以下代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
namespace BeamDesiger
{
public partial class SectionConstructor : Form
{
public static int sWidth = 0;
public static int sHeight = 0;
public static int symbolSize = 1;
public static int fontSize = 2;
public static int CsysArrowLength = 2;
public int offset = 0;
public int scaleFactor = 1;
public SectionConstructor()
{
InitializeComponent();
}
private void SectionConstructor_Load(object sender, EventArgs e)
{
MessageBox.Show("picture box loaded: w: " + pictureBox1.Width.ToString());
PictureSetup(pictureBox1);
SetWhiteCanva(pictureBox1);
DrawCSYS(pictureBox1, predefinedColors("black"));
dataGridView1.Rows.Clear();
}
public void SetWhiteCanva(PictureBox pb)
{
Bitmap bmp = new Bitmap(pb.Image);
for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
bmp.SetPixel(x, y, predefinedColors("white"));
}
}
pb.Image = bmp;
}
}
}
问题是名为
SectionConstructor_Load
' 的方法不知何故没有被读取,所以我假设整个表单没有被编译器加载,amd 首先读取 SetWhiteCanva
并在第一行崩溃 Bitmap bmp = new Bitmap(pb.Image);
为什么我认为表单未加载:
SectionConstructor_Load
方法的第一行应该返回消息,但 id 没有:MessageBox.Show("picture box loaded: w: " + pictureBox1.Width.ToString());
我检查了一下,pictureBox1 正在初始化
SectionConstructor.Designer.cs
:
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(12, 19);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(494, 351);
this.pictureBox1.TabIndex = 6;
this.pictureBox1.TabStop = false;
可能出了什么问题? 以前有用过,不知道我搞砸了什么。上面的代码是不是少了什么?我是 C# 新手,所以这可能是一件微不足道的事情。
PS:我也删除了
pictureBox1
并再次添加以确保一切就位,但仍然有问题。
异常堆栈跟踪:
at System.Drawing.Bitmap..ctor(Image original)
at BeamDesiger.SectionConstructor.SetWhiteCanva(PictureBox pb) in C:\Users\tw\source\repos\Beam Designer\BeamDesiger\BeamDesiger\SectionConstructor.cs:line 43
at BeamDesiger.SectionConstructor.dataGridViewValidator(DataGridView dgw) in C:\Users\tw\source\repos\Beam Designer\BeamDesiger\BeamDesiger\SectionConstructor.cs:line 219
at BeamDesiger.SectionConstructor.dataGridView1_CellValueChanged(Object sender, DataGridViewCellEventArgs e) in C:\Users\tw\source\repos\Beam Designer\BeamDesiger\BeamDesiger\SectionConstructor.cs:line 438
at System.Windows.Forms.DataGridView.OnCellValueChanged(DataGridViewCellEventArgs e)
at System.Windows.Forms.DataGridView.OnCellValueChangedInternal(DataGridViewCellEventArgs e)
at System.Windows.Forms.DataGridViewColumnHeaderCell.SetValue(Int32 rowIndex, Object value)
at System.Windows.Forms.DataGridViewColumn.set_HeaderText(String value)
at BeamDesiger.SectionConstructor.InitializeComponent() in C:\Users\tw\source\repos\Beam Designer\BeamDesiger\BeamDesiger\SectionConstructor.Designer.cs:line 203
at BeamDesiger.SectionConstructor..ctor() in C:\Users\tw\source\repos\Beam Designer\BeamDesiger\BeamDesiger\SectionConstructor.cs:line 29
at BeamDesiger.MainForm.button1_Click(Object sender, EventArgs e) in C:\Users\tw\source\repos\Beam Designer\BeamDesiger\BeamDesiger\MainForm.cs:line 2931
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at BeamDesiger.Program.Main() in C:\Users\tw\source\repos\Beam Designer\BeamDesiger\BeamDesiger\Program.cs:line 19
堆栈跟踪清楚地显示了 SetWhiteCanva 不是从 SectionConstructor_Load 调用的,而是从 dataGridViewValidator 调用的。如果您需要在 dataGridViewValidator 中触发 SetWhiteCanva,则很难绕过它。这里最好的解决方案是在 SetWhiteCanva 开始时检查 pictureBox1.Image 是否为 null:
public void SetWhiteCanva(PictureBox pb) {
if (pb.Image == null) return;
for (int y = 0; y < pb.Image.Height; y++)
for (int x = 0; x < pb.Image.Width; x++)
pb.Image.SetPixel(x, y, predefinedColors("white"));
}