2016-08-04 127 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

回答