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
  1. Home
  2. General Programming
  3. C#
  4. Is my program idea possible?

Is my program idea possible?

Scheduled Pinned Locked Moved C#
helpquestion
8 Posts 4 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.
  • D Offline
    D Offline
    Dioxazine
    wrote on last edited by
    #1

    I have an idea for a program using the picture viewer control. I would like to have a label at the top of the form that says "Choose a photo of the musicians listed below to see a photo." Then below that I would make four option buttons. And next to each option button put a musician's name. Then below that on the bottom have two buttons. One buttons says "Show photo." The other button says "Clear image." Problem is all the Youtube videos I've seen that demonstrate the picture box have you using open file dialog and it looks like you have to manually browse into the folder on your drive to select the photo. I don't want to do that. I want my program to be automatic. No browsing for a photo. You select an option and then hit the button "Show photo" and the program shows the photo in the viewer. But I don't know if this is possible so let me know please.

    OriginalGriffO 1 Reply Last reply
    0
    • D Dioxazine

      I have an idea for a program using the picture viewer control. I would like to have a label at the top of the form that says "Choose a photo of the musicians listed below to see a photo." Then below that I would make four option buttons. And next to each option button put a musician's name. Then below that on the bottom have two buttons. One buttons says "Show photo." The other button says "Clear image." Problem is all the Youtube videos I've seen that demonstrate the picture box have you using open file dialog and it looks like you have to manually browse into the folder on your drive to select the photo. I don't want to do that. I want my program to be automatic. No browsing for a photo. You select an option and then hit the button "Show photo" and the program shows the photo in the viewer. But I don't know if this is possible so let me know please.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Yes, it's possible: you do not need to use a OpenFileDialog to access the image, that just provides you with a path to the file. If you want to load the file from a specific location, then you can just specify that:

      byte[] data = File.ReadAllBytes(@"D:\Temp\MyImage.bmp");
      MemoryStream ms = new MemoryStream(data);
      Image im = Image.FromStream(ms);

      Or you can load from the application EXE folder:

      byte[] data = File.ReadAllBytes(@".\MyImage.bmp");
      MemoryStream ms = new MemoryStream(data);
      Image im = Image.FromStream(ms);

      This has disadvantages in that you can't modify the image reliably as the folder is likely to be read only in production. Or you can add the image as a resource and embed it in your application:

      Image im = Properties.Resources.MyImage;

      And now you can't change the image from run to run of your app at all because it is part of your application EXE file.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      D 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Yes, it's possible: you do not need to use a OpenFileDialog to access the image, that just provides you with a path to the file. If you want to load the file from a specific location, then you can just specify that:

        byte[] data = File.ReadAllBytes(@"D:\Temp\MyImage.bmp");
        MemoryStream ms = new MemoryStream(data);
        Image im = Image.FromStream(ms);

        Or you can load from the application EXE folder:

        byte[] data = File.ReadAllBytes(@".\MyImage.bmp");
        MemoryStream ms = new MemoryStream(data);
        Image im = Image.FromStream(ms);

        This has disadvantages in that you can't modify the image reliably as the folder is likely to be read only in production. Or you can add the image as a resource and embed it in your application:

        Image im = Properties.Resources.MyImage;

        And now you can't change the image from run to run of your app at all because it is part of your application EXE file.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

        D Offline
        D Offline
        Dioxazine
        wrote on last edited by
        #3

        Thanks. But I wish I understood what those lines of code mean. The only thing I understand is where you added the path to the image. That makes sense. But byte[] data and File.ReadAllBytes I don't know the why of that. And I don't know what MemoryStream ms is either. If you could perhaps explain what these lines do I would appreciate it.

        OriginalGriffO L 2 Replies Last reply
        0
        • D Dioxazine

          Thanks. But I wish I understood what those lines of code mean. The only thing I understand is where you added the path to the image. That makes sense. But byte[] data and File.ReadAllBytes I don't know the why of that. And I don't know what MemoryStream ms is either. If you could perhaps explain what these lines do I would appreciate it.

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Oh come on! Are you trying to run before you can walk? This is pretty basic stuff! You know what a byte[] is, don't you? It's an array of bytes, is all. So what do you think that File.ReadAllBytes does? And if you don't understand what a function does, look it up in the official documentation: File.ReadAllBytes(String) Method (System.IO) | Microsoft Docs[^] The next two are there for a specific reason: while you can load an image directly from a file, that locks the file until the image is Disposed - so if you want to write a modified version back to the file in your app, you can't. Converting the data to a Stream gets round that, by locking the Stream instead of the file. And if you don't understand what a Stream is then how on earth are you doing any file input / output? :laugh: So those three lines do this: 1) Read the disk-based image file into an array of bytes. 2) Construct a memory-based Stream from the array. 3) Create an Image from the Stream. Seriously, if you don't understand stuff like this, then you need to re-read your course notes from the beginning, because you have missed a heck of a lot of useful stuff!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          D 1 Reply Last reply
          0
          • D Dioxazine

            Thanks. But I wish I understood what those lines of code mean. The only thing I understand is where you added the path to the image. That makes sense. But byte[] data and File.ReadAllBytes I don't know the why of that. And I don't know what MemoryStream ms is either. If you could perhaps explain what these lines do I would appreciate it.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You should just use a "picture viewer". Something like MS Photo. It illustrates concepts such as files and folders.

            It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              Oh come on! Are you trying to run before you can walk? This is pretty basic stuff! You know what a byte[] is, don't you? It's an array of bytes, is all. So what do you think that File.ReadAllBytes does? And if you don't understand what a function does, look it up in the official documentation: File.ReadAllBytes(String) Method (System.IO) | Microsoft Docs[^] The next two are there for a specific reason: while you can load an image directly from a file, that locks the file until the image is Disposed - so if you want to write a modified version back to the file in your app, you can't. Converting the data to a Stream gets round that, by locking the Stream instead of the file. And if you don't understand what a Stream is then how on earth are you doing any file input / output? :laugh: So those three lines do this: 1) Read the disk-based image file into an array of bytes. 2) Construct a memory-based Stream from the array. 3) Create an Image from the Stream. Seriously, if you don't understand stuff like this, then you need to re-read your course notes from the beginning, because you have missed a heck of a lot of useful stuff!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

              D Offline
              D Offline
              Dioxazine
              wrote on last edited by
              #6

              Come on now. I only program as a hobby. I've only done console programs so far. There is a lot of stuff I have not done. You programmers need to have some more understanding of what it's like not to be a full time developer. If you came to me for guitar lessons I too could slice you to ribbons for all that you don't know. But that is a very poor way of teaching guitar. Why would I need a byte array anyway. Just load the photo into the viewer. My photos will not be changing.

              OriginalGriffO L 2 Replies Last reply
              0
              • D Dioxazine

                Come on now. I only program as a hobby. I've only done console programs so far. There is a lot of stuff I have not done. You programmers need to have some more understanding of what it's like not to be a full time developer. If you came to me for guitar lessons I too could slice you to ribbons for all that you don't know. But that is a very poor way of teaching guitar. Why would I need a byte array anyway. Just load the photo into the viewer. My photos will not be changing.

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                Dioxazine wrote:

                If you came to me for guitar lessons I too could slice you to ribbons for all that you don't know.

                Yes, because I know nothing.

                Dioxazine wrote:

                But that is a very poor way of teaching guitar.

                And so is what you are doing. Suppose I came to you for help with my guitar playing, and told you that I was learning on my own by guessing what to do and wondering why I can't play as fast as others - but the forks and spoons I jam under the frets to select the notes to strum take me too long to pull out and move? Clearly, I don't know enough to play the guitar because I've spent so long guessing what to do instead of sitting down and actually learning the right way! And that's where you are with development. You've found code that does some of what you want, but you don't understand how it works - or even exactly what it is doing - so you can't change it to do something else yourself. And for the same reasons as me with my guitar spoons - they work for me, but not very well - I need to be taken back to the beginning and learn how to play in a structured way that introduces chords and so on in a logical progression. Seriously, even as a hobby this is a complicated subject and it really benefits from learning the basics thoroughly before you try to rush ahead to "more interesting" projects.

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                1 Reply Last reply
                0
                • D Dioxazine

                  Come on now. I only program as a hobby. I've only done console programs so far. There is a lot of stuff I have not done. You programmers need to have some more understanding of what it's like not to be a full time developer. If you came to me for guitar lessons I too could slice you to ribbons for all that you don't know. But that is a very poor way of teaching guitar. Why would I need a byte array anyway. Just load the photo into the viewer. My photos will not be changing.

                  L Offline
                  L Offline
                  Liktor Janos
                  wrote on last edited by
                  #8

                  Why do you need stream to load picture into viewer? Just imagine this: You(stream) are necessary to play(load) a song(picture) at your guitar(viewer).

                  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