2017-08-28 45 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