2016-08-04 123 views
1

我正在使用WinForms。我正在嘗試製作一個簡單的圖像瀏覽器。在我的表格中,我有一個PictureBox。我希望能夠右鍵單擊我的計算機中的任何圖片,單擊「打開方式」,從列表中單擊我的應用程序,圖片將在我的應用程序中打開。使用打開方式在圖片框中打開圖片

[DllImport("Shell32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2); 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    public static bool IsAssociated() 
    { 
     return (Registry.CurrentUser.OpenSubKey("Software\\Classes\\.tif", false) == null); 
    } 

    public static void Associate() 
    { 
     RegistryKey FileReg = Registry.CurrentUser.CreateSubKey("Software\\Classes\\.tif"); 
     RegistryKey AppReg = Registry.CurrentUser.CreateSubKey("Software\\Classes\\Application\\How_To_Open_File_With_Associated_Applicaiton.exe"); 
     RegistryKey AppAssoc = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.tif"); 

     FileReg.CreateSubKey("DefaultIcon").SetValue("", "C:\\Users\\Timmy\\Pictures\\Icons_Folder\\I-View-Icon_V2.ico");//Path to icon 
     FileReg.CreateSubKey("PerceivedType").SetValue("", "image"); //Something that the file holds. 

     AppReg.CreateSubKey("shell\\open\\command").SetValue("", "\"" + Application.ExecutablePath + "\" %"); 
     AppReg.CreateSubKey("shell\\edit\\command").SetValue("", "\"" + Application.ExecutablePath + "\" %"); 
     AppReg.CreateSubKey("DefaultIcon").SetValue("", "C:\\Users\\Timmy\\Pictures\\Icons_Folder\\I-View-Icon_V2.ico"); 

     AppAssoc.CreateSubKey("UserChoice").SetValue("Progid", "Application\\How_To_Open_File_With_Associated_Applicaiton.exe"); 
     SHChangeNotify(0x08000000 , 0x0000, IntPtr.Zero, IntPtr.Zero); 

    } 

例子: 在下面,我開油漆圖片的演示。

目標: 在我的應用程序中打開.tif圖像到我picturebox的能力。

參考
這些是一些例子,我在網上進行搜索,試圖做我想要做的任務,但是我發現我自己陷入如何告訴程序,「現在借這個.tif圖片,並在圖片框打開它「:

Associate File Extension with Application

https://www.youtube.com/watch?v=XtYobuVvcFE

http://www.codeproject.com/Articles/43675/C-FileAssociation-Class


enter image description here

+0

您有問題嗎? – Plutonix

+0

是的,我想知道如何打開圖像從我如何描述到我的圖片框。 @Plutonix – taji01

回答