2011-04-16 116 views
1

我的功能是從選定的文件夾中加載圖片。現在我想通過以新形式打開ZoomLogo來縮放此圖片。在這種新形式中,我想從我的主窗體中'獲取'fullPath1,然後使用ZoomLogo窗體中的此路徑加載圖片。如何做到這一點?如何在C#中從一種表單獲取數據到另一種表單#

void Picture() 
{ 

...

if (DataBaseSelection.SelectedIndex+1==1) 
    { 
    Logo1_pictureBox.Image=new Bitmap(@"Logos\\aa.bmp"); 
    var file1 = Path.ChangeExtension(Printer2_TextBox.Text, ".jpg"); 
    var fullPath1 = Path.Combine(@"Documents\\Base\\", file1); 
    if (!File.Exists(fullPath1)) 
    { 
    MessageBox.Show("No picture!"); 
    } 
    else 
    { 
     Logo_pictureBox.Image = new Bitmap(fullPath1); 
    } 

...

} 

打開新的形式:

void ZoomPictureBoxClick(object sender, EventArgs e) 
    { 
     ZoomSchematic settings = new ZoomSchematic(); 
     settings.ShowDialog();   
    } 

我試圖用類似的東西在我的主要形式:

void ZoomPictureBoxClick(object sender, EventArgs e) 
    { 
     ZoomSchematic settings = new ZoomSchematic(this.fullPath1); 
     settings.ShowDialog();   
    } 

,但我不知道如何從功能圖像()這個變量..

+0

您是否需要從ZoomSchematic對話框中獲取指定的設置? – 2011-04-16 09:37:20

+0

我需要的是fullPath1,所以我可以使用此位置路徑顯示圖片。 ZoomSchematic對話框只包含1個圖片框。 – Elfoc 2011-04-16 09:39:37

回答

0

只需使fullPath1成爲您主表單的成員字段即可。

class MainForm 
{ 
    private string fullPath1; 

    void Picture() 
    { 
     if (DataBaseSelection.SelectedIndex+1==1) 
     { 
      Logo1_pictureBox.Image=new Bitmap(@"Logos\\aa.bmp"); 
      var file1 = Path.ChangeExtension(Printer2_TextBox.Text, ".jpg"); 
      fullPath1 = Path.Combine(@"Documents\\Base\\", file1); 
      if (!File.Exists(fullPath1)) 
      { 
       MessageBox.Show("No picture!"); 
      } 
      else 
      { 
       Logo_pictureBox.Image = new Bitmap(fullPath1); 
      } 
     } 
     } 

    void ZoomPictureBoxClick(object sender, EventArgs e) 
    { 
     ZoomSchematic settings = new ZoomSchematic(this.fullPath1); 
     settings.ShowDialog();   
    } 
} 

class ZoomSchematic 
{ 
    string _fullPath1; 

    public ZoomSchematic(string fullPath1) 
    { 
     _fullPath1 = fullPath1; 
    } 
} 
+0

我得到錯誤:錯誤CS1729:'App.ZoomSchematic'不包含一個構造函數,需要1個參數 – Elfoc 2011-04-16 09:51:57

+0

請參閱上面的編輯。 – briantyler 2011-04-16 09:56:55

+0

這是放這個類的正確位置嗎?:namespace App { \t class ZoomSchematic {...} \t public partial class MainForm:Form {..} cause i'm getting error:The item「obj \ x86 \ Release \ App.ZoomSchematic.resources「在」資源「參數中指定了多次。 「資源」參數不支持重複的項目。 – Elfoc 2011-04-16 10:06:20

0

如果你究竟要放大,爲什麼不將圖片發送,而不是重新加載圖片?例如,

void ZoomPictureBoxClick(Bitmap zoomthis) 
{ 
... 
} 

,因爲這將是你的形式還是內,例如

ZoomSchematic settings = new ZoomSchematic(Logo_pictureBox.Image) 

這取決於你想用它做一次那裏,如果你只是要顯示的設置是什麼一點點,我想這取決於你爲什麼需要這條路。

一個大的答案的位,但我覺得這個問題是開放的更多的問題。

如果你需要稍後發送的路徑,那麼當你創建你的位圖時,你可以/應該保持路徑。而不是局部變量。

+0

ZoomSchematic設置=新ZoomSchematic(Logo_pictureBox.Image); settings.ShowDialog();和我gettin錯誤:錯誤CS1729:'App.ZoomSchematic'不包含帶1個參數的構造函數。 ZoomSchematic由我順便創建。 – Elfoc 2011-04-16 09:56:25

+0

好吧,那麼你需要創建一個允許參數的構造函數,這就是爲什麼它的抱怨 – BugFinder 2011-04-16 09:58:45

+0

該死的..非常多的學習..:) – Elfoc 2011-04-16 10:08:57

0

使用形式的構造函數傳遞文件路徑

Form2 form2 = new Form2(string path); 
Form2.ShowDialog(); 
0

要看是什麼Picture()不和它叫什麼......如果畫面提供了多張圖片到你的主要形式,你將不得不排序哪些是有問題的圖片...

如果只有一張圖片,你可以簡單地一個新的私人字符串成員fullpath1添加到您的形式,將不得不每次Picture()設置叫...

相關問題