Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Point out the error

Point out the error

Scheduled Pinned Locked Moved C#
helpcsharpasp-netjson
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S sns

    hi friends, i want to add an image to commandbarbutton in C#. i have added appropriate code but im getting an error saying that "object reference not set to an instance of an object" when im trying to assign an picture to commanbarbutton.Picture property. here is my sample code: CommandBars objCommandBars; CommandBar objCommandBar; object str = "MyToolBar"; objCommandBars = (CommandBars)applicationObject.GetType().InvokeMember("CommandBars", BindingFlags.GetProperty , null, applicationObject ,null); objCommandBars["MyToolBar"].Delete(); objCommandBars.Add(str,omissing,omissing,omissing); objCommandBars["MyToolBar"].Position = MsoBarPosition.msoBarFloating; cmdOpenI = (CommandBarButton)objCommandBar.Controls.Add(1,omissing,omissing,omissing,omissing); cmdOpenI.Caption = "Test"; cmdOpenI.Tag = "Test"; String strFile = "F:\\a1.bmp"; Object lpDisp = null; try { OleLoadPictureFile((Object) strFile, out lpDisp); //API relevant to LoadPicture in VB stdole.StdPicture pic = (stdole.StdPicture)lpDisp; if (pic != null) { cmdOpenI.Style = MsoButtonStyle.msoButtonIconAndCaption; cmdOpenI.Picture = (stdole.IPictureDisp)pic;// here only im getting an error "object reference not set to an instance of an object" } } catch(Exception exp) { System.Windows.Forms.MessageBox.Show(exp.Message); } cmdOpenI.Visible = true; cmdOpenI.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(OpenIntranet_Click); objCommandBars = null; objCommandBar = null; if i set the commandbutton type as 2,3 otherthan button type 1 im not getting an error. but i want the button type to be 1. can anybody help me to achieve the task. Thanks in advance regards, sns

    J Offline
    J Offline
    Jeff J
    wrote on last edited by
    #2

    Just a guess here, but since the code fails only when a picture is absolutely required (type 1 is strictly icon, whereas other two use text and thus don't require a picture to be shown), maybe the error is saying that pic's underlying image is always invalid, and that only matters when one is absolutely needed. In other words, although pic is not null, it may not contain a reference to a valid StdPicture. Since StdPicture is a COM object, it is possible that it is non-null, yet its Handle property may be invalid. COM objects can be that way. Could you try verifying if pic.Handle is valid rather than if pic overall is non-null?

    S 2 Replies Last reply
    0
    • J Jeff J

      Just a guess here, but since the code fails only when a picture is absolutely required (type 1 is strictly icon, whereas other two use text and thus don't require a picture to be shown), maybe the error is saying that pic's underlying image is always invalid, and that only matters when one is absolutely needed. In other words, although pic is not null, it may not contain a reference to a valid StdPicture. Since StdPicture is a COM object, it is possible that it is non-null, yet its Handle property may be invalid. COM objects can be that way. Could you try verifying if pic.Handle is valid rather than if pic overall is non-null?

      S Offline
      S Offline
      sns
      wrote on last edited by
      #3

      an error. am i correct? if so im getting 8 digit +ve number when i read the handle. im not able to locate still whats an error. regards, sns

      1 Reply Last reply
      0
      • J Jeff J

        Just a guess here, but since the code fails only when a picture is absolutely required (type 1 is strictly icon, whereas other two use text and thus don't require a picture to be shown), maybe the error is saying that pic's underlying image is always invalid, and that only matters when one is absolutely needed. In other words, although pic is not null, it may not contain a reference to a valid StdPicture. Since StdPicture is a COM object, it is possible that it is non-null, yet its Handle property may be invalid. COM objects can be that way. Could you try verifying if pic.Handle is valid rather than if pic overall is non-null?

        S Offline
        S Offline
        sns
        wrote on last edited by
        #4

        Actually what i understood from handle means it will return -1 if it is an error. am i correct? if so im getting 8 digit +ve number when i read the handle. im not able to locate still whats an error. regards, sns

        J 1 Reply Last reply
        0
        • S sns

          Actually what i understood from handle means it will return -1 if it is an error. am i correct? if so im getting 8 digit +ve number when i read the handle. im not able to locate still whats an error. regards, sns

          J Offline
          J Offline
          Jeff J
          wrote on last edited by
          #5

          I am not so sure it would return -1 if the handle is invalid: Console.WriteLine(pic.Handle); It is difficult to determine what is valid here, since there is such a mixture of COM Automation and .Net, but you might try tracing back a little further. Did you try determining if lpDisp is valid before assigning it to pic? I cannot find a reference to OleLoadPictureFile(), so I am not sure if it might be any cause of trouble. The main thing is to verify that pic is a valid object, and that all its properties and methods can be called. Another test you might try is: cmdOpenI.Picture = (stdole.IPictureDisp)lpDisp; That bypasses any reference to pic.

          S 1 Reply Last reply
          0
          • J Jeff J

            I am not so sure it would return -1 if the handle is invalid: Console.WriteLine(pic.Handle); It is difficult to determine what is valid here, since there is such a mixture of COM Automation and .Net, but you might try tracing back a little further. Did you try determining if lpDisp is valid before assigning it to pic? I cannot find a reference to OleLoadPictureFile(), so I am not sure if it might be any cause of trouble. The main thing is to verify that pic is a valid object, and that all its properties and methods can be called. Another test you might try is: cmdOpenI.Picture = (stdole.IPictureDisp)lpDisp; That bypasses any reference to pic.

            S Offline
            S Offline
            sns
            wrote on last edited by
            #6

            i tried out all posiblities, and i think the out object of OleLoadPictureFile() is not returning correct format, because when i loaded with someother bmp and tried to get the type it is giving Object refernce not set to an instance of an object... dont know the reason, the same error is not coming for a1.bmp when i tried to read the type of pic. can u suggest me someother method to load picture or with the existing one with change of some properties... looking for a postive reply from u. Thanking you. regards sns

            1 Reply Last reply
            0
            • S sns

              hi friends, i want to add an image to commandbarbutton in C#. i have added appropriate code but im getting an error saying that "object reference not set to an instance of an object" when im trying to assign an picture to commanbarbutton.Picture property. here is my sample code: CommandBars objCommandBars; CommandBar objCommandBar; object str = "MyToolBar"; objCommandBars = (CommandBars)applicationObject.GetType().InvokeMember("CommandBars", BindingFlags.GetProperty , null, applicationObject ,null); objCommandBars["MyToolBar"].Delete(); objCommandBars.Add(str,omissing,omissing,omissing); objCommandBars["MyToolBar"].Position = MsoBarPosition.msoBarFloating; cmdOpenI = (CommandBarButton)objCommandBar.Controls.Add(1,omissing,omissing,omissing,omissing); cmdOpenI.Caption = "Test"; cmdOpenI.Tag = "Test"; String strFile = "F:\\a1.bmp"; Object lpDisp = null; try { OleLoadPictureFile((Object) strFile, out lpDisp); //API relevant to LoadPicture in VB stdole.StdPicture pic = (stdole.StdPicture)lpDisp; if (pic != null) { cmdOpenI.Style = MsoButtonStyle.msoButtonIconAndCaption; cmdOpenI.Picture = (stdole.IPictureDisp)pic;// here only im getting an error "object reference not set to an instance of an object" } } catch(Exception exp) { System.Windows.Forms.MessageBox.Show(exp.Message); } cmdOpenI.Visible = true; cmdOpenI.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(OpenIntranet_Click); objCommandBars = null; objCommandBar = null; if i set the commandbutton type as 2,3 otherthan button type 1 im not getting an error. but i want the button type to be 1. can anybody help me to achieve the task. Thanks in advance regards, sns

              W Offline
              W Offline
              WizardOfPeyton
              wrote on last edited by
              #7

              I am also trying to do something similiar, where did OleLoadPictureFile(...) come from. Did you add a referenceto the project to make it available. I can't find any documentation on that method. If you could let me know I will dig into this further.

              S 1 Reply Last reply
              0
              • W WizardOfPeyton

                I am also trying to do something similiar, where did OleLoadPictureFile(...) come from. Did you add a referenceto the project to make it available. I can't find any documentation on that method. If you could let me know I will dig into this further.

                S Offline
                S Offline
                sns
                wrote on last edited by
                #8

                im happy to hear that u r also trying to do same thing what im trying,so i'll be getting some help regarding this. this how u have to declare an API [DllImport("oleaut32.dll", CharSet = CharSet.Unicode, PreserveSig = true, EntryPoint = "OleLoadPictureFile")] static extern void OleLoadPictureFile(Object varObj,[ MarshalAs(UnmanagedType.IDispatch )] out Object oOut); so when u r referring this automatically u can call the method, in query itself i have specified how to call, u can refer the same. regarding help even i was not able to locate the same, no documentation, the thing is if it is VB so many documents available, but for C# no help. for stdole u have to add a reference to stdole. try this. in case any pbm reply to me. Thanks. regards, sns

                W 1 Reply Last reply
                0
                • S sns

                  im happy to hear that u r also trying to do same thing what im trying,so i'll be getting some help regarding this. this how u have to declare an API [DllImport("oleaut32.dll", CharSet = CharSet.Unicode, PreserveSig = true, EntryPoint = "OleLoadPictureFile")] static extern void OleLoadPictureFile(Object varObj,[ MarshalAs(UnmanagedType.IDispatch )] out Object oOut); so when u r referring this automatically u can call the method, in query itself i have specified how to call, u can refer the same. regarding help even i was not able to locate the same, no documentation, the thing is if it is VB so many documents available, but for C# no help. for stdole u have to add a reference to stdole. try this. in case any pbm reply to me. Thanks. regards, sns

                  W Offline
                  W Offline
                  WizardOfPeyton
                  wrote on last edited by
                  #9

                  I think I found what you are looking for. This was posted at devcity.com under CommandBarButton Picture thread. I don't want to take credit for the solution. Here is what was posted. I was attempting to change the icon displayed on a tool window using the ToolWindow.SetTabPicture(), which needed a IPicture object. Make sure your bitmap is the correct size. I was getting a parameter incorrect exception until I corrected the size of my bitmap. Hope this helps. _commandBarButton.Style = MsoButtonStyle.msoButtonIconAndCaption; //put an icon ont he button Image _icon = System.Drawing.Image.FromFile("c:\\cloud.ico"); _commandBarButton.Picture = WService.ImageConverter.ImageToIpicture(_icon); using System; namespace WService { /// /// Summary description for ImageConverter. /// public class ImageConverter : System.Windows.Forms.AxHost { public ImageConverter():base("59EE46BA-677D-4d20-BF10-8D8067CB8B33") { } public static stdole.IPictureDisp ImageToIpicture(System.Drawing.Image image) { return (stdole.IPictureDisp)ImageConverter.GetIPictureDispFromPicture(image); } public static System.Drawing.Image IPictureToImage(stdole.StdPicture picture) { return ImageConverter.GetPictureFromIPicture(picture); } } }

                  S 1 Reply Last reply
                  0
                  • W WizardOfPeyton

                    I think I found what you are looking for. This was posted at devcity.com under CommandBarButton Picture thread. I don't want to take credit for the solution. Here is what was posted. I was attempting to change the icon displayed on a tool window using the ToolWindow.SetTabPicture(), which needed a IPicture object. Make sure your bitmap is the correct size. I was getting a parameter incorrect exception until I corrected the size of my bitmap. Hope this helps. _commandBarButton.Style = MsoButtonStyle.msoButtonIconAndCaption; //put an icon ont he button Image _icon = System.Drawing.Image.FromFile("c:\\cloud.ico"); _commandBarButton.Picture = WService.ImageConverter.ImageToIpicture(_icon); using System; namespace WService { /// /// Summary description for ImageConverter. /// public class ImageConverter : System.Windows.Forms.AxHost { public ImageConverter():base("59EE46BA-677D-4d20-BF10-8D8067CB8B33") { } public static stdole.IPictureDisp ImageToIpicture(System.Drawing.Image image) { return (stdole.IPictureDisp)ImageConverter.GetIPictureDispFromPicture(image); } public static System.Drawing.Image IPictureToImage(stdole.StdPicture picture) { return ImageConverter.GetPictureFromIPicture(picture); } } }

                    S Offline
                    S Offline
                    sns
                    wrote on last edited by
                    #10

                    Hi, Thanx for the help given. i implemented the same method earlier, but im getting the same error. still im not getting the reason for that error, bcoz both methods OleloadPictureFile as well as GetIPictureDispFromPicture gives the proper image, the only thing is im not able to assign this to a commandbarbutton. im using shared addins through which im communicating to Microsoft Office 2000, in that im adding an customized commandbarbutton. I have referred Microsoft Office 9.0 object Library and stdole for picture. To the commandbarbutton im able to add events, text and other so many properties except Picture and Mask... Can u tell the reason why this error is coming, is any reference missing?. regards, sns:confused:

                    W 1 Reply Last reply
                    0
                    • S sns

                      Hi, Thanx for the help given. i implemented the same method earlier, but im getting the same error. still im not getting the reason for that error, bcoz both methods OleloadPictureFile as well as GetIPictureDispFromPicture gives the proper image, the only thing is im not able to assign this to a commandbarbutton. im using shared addins through which im communicating to Microsoft Office 2000, in that im adding an customized commandbarbutton. I have referred Microsoft Office 9.0 object Library and stdole for picture. To the commandbarbutton im able to add events, text and other so many properties except Picture and Mask... Can u tell the reason why this error is coming, is any reference missing?. regards, sns:confused:

                      W Offline
                      W Offline
                      WizardOfPeyton
                      wrote on last edited by
                      #11

                      I will attempt to look at this again. The example code you initialy listed includes 'omissing' as the parameter to various methods. Where is this defined?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups