2017-08-28 96 views
0

我有一个成功的构建使用一些截图代码,并得到拖放工作(但没有代码从那里),我试图显示一条消息在文件保存成功后,或者如果您拖放太多文件后,形式为。我不想使用MessageBox.Show(即使我替换代码,此方法也可以),因为我想使用复选标记图片和按钮来打开屏幕截图位置的文件。我的问题是屏幕截图保存后,from#.designer.cs崩溃。我已经看遍了如何解决这个问题,并没有帮助。我不注意触摸Form3.Designer.cs文件,因为评论说不要。我的代码列在下面。在表1C# - DisplayDialog()不工作FileSave和拖放

截图/屏幕截图代码:

 private void saveScreenshotToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     // ** SAVE A SCREENSHOT *** (Working) 
     Bitmap bitmap = new Bitmap(this.Width, this.Height); 
     DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height)); 
     bitmap.Save((boxFileName.Text) + "_ScreenCap.JPEG",ImageFormat.Jpeg); 

     // If i replace the following 2 lines with MessageBox.Show("FileSave successful"); it works fine. Why is this code not working???? 

     Form3 f3 = new Form3(); 
     f3.ShowDialog(); 

    } 

代码,在Form3.Designer.CS

 // Form3 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(292, 121); 
     this.Controls.Add(this.button1); 
     this.Controls.Add(this.label2); 
     this.Controls.Add(this.label1); 
     this.Controls.Add(this.pictureBox1); 
     this.MaximizeBox = false; 
     this.MaximumSize = new System.Drawing.Size(300, 150); 
     this.MinimizeBox = false; 
     this.MinimumSize = new System.Drawing.Size(300, 150); 
     this.Name = "Form3"; 
     this.ShowIcon = false; 
     this.ShowInTaskbar = false; 
     this.Text = "ScreenCap Result"; 
     this.Load += new System.EventHandler(this.Form3_Load); 

     /// the following line crashed 

     ((System.ComponentModel.ISupportInitialize)(this.performanceCounter1)).EndInit(); 

     // end line that crashed 

     ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); 
     this.ResumeLayout(false); 
     this.PerformLayout(); 

这里崩溃是从的DragDrop代码:

void Form1_DragDrop(object sender, DragEventArgs e) 
    { 
     var files = (string[])e.Data.GetData(DataFormats.FileDrop); 
     if (files.Length == 1) 
     { 
      /// WORKING 

      MessageBox.Show("Reading files from Drag and Drop not fully implimentd."); 
     } 
     else 
     { 
      // NOT WORING (crashes) 

      Form3 f3 = new Form3(); 
      f3.ShowDialog(); 
     } 

任何帮助将是赞赏。

+0

那么,什么异常导致它崩溃?文本告诉你什么? –

+0

您可以在设计器中打开Form3.cs,还是会崩溃? –

+0

@ThomasWeller号码成功编译。它只会在应用程序运行时事件触发时崩溃。 –

回答

0

我想通了什么导致了问题。我不得不从嵌入对象窗口中删除perfomanceCounter1对象,并且应用程序崩溃的问题消失了。我必须更加小心我为嵌入对象添加的内容。

0

你链接到屏幕截图该错误消息说

出现InvalidOperationException

其他信息:初始化失败,因为该类别名称缺失。

Part of the screenshot provided by OP

看来微软忘记记录的EndInit()呼叫例外,不过这不是第一个。

因此,在performanceCounter1,我认为是PerformanceCounter类型,您需要提供一个值为属性CategoryName

你可以做到这一点,而无需手动修改Form3.Designer.cs文件。取而代之的是,在设计开放Form3,选择performanceCounter1并选择以显示在下面的屏幕截图从列表中值:

Select a value from the list of performance counters