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. How can I load an image in C#

How can I load an image in C#

Scheduled Pinned Locked Moved C#
questioncsharp
11 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.
  • A Offline
    A Offline
    apoll
    wrote on last edited by
    #1

    Hi at all. I made a program which is able to modify images. Now I want it like this: You choose an image in your explorer, click the right button of your mouse and click "open with" and select my program, which I called PhotoClient. Ok, so the PhotoClient is starting but the selected image is not in the picturebox1 of my PhotoClient. So how can I make this come true that I can load an image like this ?

    J 1 Reply Last reply
    0
    • A apoll

      Hi at all. I made a program which is able to modify images. Now I want it like this: You choose an image in your explorer, click the right button of your mouse and click "open with" and select my program, which I called PhotoClient. Ok, so the PhotoClient is starting but the selected image is not in the picturebox1 of my PhotoClient. So how can I make this come true that I can load an image like this ?

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      The file path to the image file will be passed to your program's arguments inside your main method. Inside Program.cs, you probably have a method like this:

      static void Main()
      {
      ...
      }

      Change that to

      static void Main(string[] arguments)
      {
         ...
      }

      When your app is launched, look at the arguments parameter. It should contain the file path to the image that Windows Explorer passed to you.

      Tech, life, family, faith: Give me a visit. I'm currently blogging about: The Lord Is So Good The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

      A 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        The file path to the image file will be passed to your program's arguments inside your main method. Inside Program.cs, you probably have a method like this:

        static void Main()
        {
        ...
        }

        Change that to

        static void Main(string[] arguments)
        {
           ...
        }

        When your app is launched, look at the arguments parameter. It should contain the file path to the image that Windows Explorer passed to you.

        Tech, life, family, faith: Give me a visit. I'm currently blogging about: The Lord Is So Good The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

        A Offline
        A Offline
        apoll
        wrote on last edited by
        #3

        Thanks for your help. I put the "string[] arguments" in the Main, and now ? What do you mean with "look at the arguments" ? How can I transfer the file path into my Form1 ?

        S 1 Reply Last reply
        0
        • A apoll

          Thanks for your help. I put the "string[] arguments" in the Main, and now ? What do you mean with "look at the arguments" ? How can I transfer the file path into my Form1 ?

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

          Change the default constructor to take a string array, then change the line "new Form1();" to "new Form1(arguments);". Then in your constructor, use a foreach to try and open the files specified. Jeff

          A 1 Reply Last reply
          0
          • S Skippums

            Change the default constructor to take a string array, then change the line "new Form1();" to "new Form1(arguments);". Then in your constructor, use a foreach to try and open the files specified. Jeff

            A Offline
            A Offline
            apoll
            wrote on last edited by
            #5

            sorry Jeff, I do not really understand, I am new in C#. So, this is my Program.CS using System; using System.Collections.Generic; using System.Windows.Forms; namespace PhotoClient { static class Program { /// /// Der Haupteinstiegspunkt für die Anwendung. /// [STAThread] static void Main(string[] arguments) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } } And here comes the Form1 // Photo Client 20 // using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Drawing.Imaging; namespace PhotoClient { public partial class Form1 : Form { string[] arguments; public Form1() { InitializeComponent(); } #region variables Bitmap original_image, rot_image, carry, image; bool fill = false; string extension = ""; float resize = 1; OpenFileDialog load = new OpenFileDialog(); BrightnessAdjuster ba = new BrightnessAdjuster(); Rotation rt = new Rotation(); Draw dr = new Draw(); Save_percent sav = new Save_percent(); #endregion variables #region load n initialize private void load_Click(object sender, EventArgs e) { load.Filter = " jpg files (*.jpg)|*.jpg| All files (*.*)|*.*| bmp files (*.bmp)|*.bmp| gif files (*.gif)|*.gif "; if (load.ShowDialog() == DialogResult.OK) { initialize(); } } public void initialize() { image = new Bitmap(load.FileName); FileInfo original = new FileInfo(load.FileName); extension = original.Name; trackBar1.Maximum = image.Width; trackBar2.Maximum = image.Width; ... ... ... So what is a "constructor" ?

            M 1 Reply Last reply
            0
            • A apoll

              sorry Jeff, I do not really understand, I am new in C#. So, this is my Program.CS using System; using System.Collections.Generic; using System.Windows.Forms; namespace PhotoClient { static class Program { /// /// Der Haupteinstiegspunkt für die Anwendung. /// [STAThread] static void Main(string[] arguments) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } } And here comes the Form1 // Photo Client 20 // using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Drawing.Imaging; namespace PhotoClient { public partial class Form1 : Form { string[] arguments; public Form1() { InitializeComponent(); } #region variables Bitmap original_image, rot_image, carry, image; bool fill = false; string extension = ""; float resize = 1; OpenFileDialog load = new OpenFileDialog(); BrightnessAdjuster ba = new BrightnessAdjuster(); Rotation rt = new Rotation(); Draw dr = new Draw(); Save_percent sav = new Save_percent(); #endregion variables #region load n initialize private void load_Click(object sender, EventArgs e) { load.Filter = " jpg files (*.jpg)|*.jpg| All files (*.*)|*.*| bmp files (*.bmp)|*.bmp| gif files (*.gif)|*.gif "; if (load.ShowDialog() == DialogResult.OK) { initialize(); } } public void initialize() { image = new Bitmap(load.FileName); FileInfo original = new FileInfo(load.FileName); extension = original.Name; trackBar1.Maximum = image.Width; trackBar2.Maximum = image.Width; ... ... ... So what is a "constructor" ?

              M Offline
              M Offline
              MCEdwards
              wrote on last edited by
              #6

              Apoll The contructor is run when the a class is instantiated. A constructor has the same name as the class. In your code the constructor is public Form1(){ InitializeComponent(); } What you will want to do is pass in the file name from the arguments list into this constructor. Modify the constructor to accept the path as a string like so: public Form1(string ImagePath){ InitializeComponent(); } Then in the main method pass the path to the new form like so: static void Main(string[] arguments) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(arguments[0])); } I have assumed that the path is stored as the first item in the array. You can then call the initialize method and pass in the path to the file (you will of course have to modify your initialize method to accept the file path as a parameter. Hope this helps. :)

              A 1 Reply Last reply
              0
              • M MCEdwards

                Apoll The contructor is run when the a class is instantiated. A constructor has the same name as the class. In your code the constructor is public Form1(){ InitializeComponent(); } What you will want to do is pass in the file name from the arguments list into this constructor. Modify the constructor to accept the path as a string like so: public Form1(string ImagePath){ InitializeComponent(); } Then in the main method pass the path to the new form like so: static void Main(string[] arguments) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(arguments[0])); } I have assumed that the path is stored as the first item in the array. You can then call the initialize method and pass in the path to the file (you will of course have to modify your initialize method to accept the file path as a parameter. Hope this helps. :)

                A Offline
                A Offline
                apoll
                wrote on last edited by
                #7

                Ok, thank you very much, it is working. Apoll

                S 1 Reply Last reply
                0
                • A apoll

                  Ok, thank you very much, it is working. Apoll

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

                  Be careful with that, because it REQUIRES at least one argument, and ignores all others. If you want to be able to run from the command line OR open the application through explorer, you need to either... 1. Send the entire argument array into the constructor, or 2. Put an if, else in your static main method to determine whether to call the initializer with parameters or without If you only want this to run when you double click a file (or specify a file at the command line) then ignore this post. Jeff

                  A 1 Reply Last reply
                  0
                  • S Skippums

                    Be careful with that, because it REQUIRES at least one argument, and ignores all others. If you want to be able to run from the command line OR open the application through explorer, you need to either... 1. Send the entire argument array into the constructor, or 2. Put an if, else in your static main method to determine whether to call the initializer with parameters or without If you only want this to run when you double click a file (or specify a file at the command line) then ignore this post. Jeff

                    A Offline
                    A Offline
                    apoll
                    wrote on last edited by
                    #9

                    oh, yeprs, this is exactly my problem now. The thing is, when I make it with if else like this: public partial class Form1 : Form { Bitmap original_image; string path; public Form1(string[] ImagePath) { InitializeComponent(); if (ImagePath[0] != null) { path = ImagePath[0]; original_image = new Bitmap(path); pictureBox1.Image = original_image; } } it says that "if (ImagePath[0] != null)" is outside the bounds of the array

                    S 1 Reply Last reply
                    0
                    • A apoll

                      oh, yeprs, this is exactly my problem now. The thing is, when I make it with if else like this: public partial class Form1 : Form { Bitmap original_image; string path; public Form1(string[] ImagePath) { InitializeComponent(); if (ImagePath[0] != null) { path = ImagePath[0]; original_image = new Bitmap(path); pictureBox1.Image = original_image; } } it says that "if (ImagePath[0] != null)" is outside the bounds of the array

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

                      Just do this... static class Program { [STAThread] static void Main(string[] arguments) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(arguments)); } } public partial class Form1 : Form { Bitmap m_Img null; public Form1(string[] arguments) { InitializeComponent(); if (arguments != null) { foreach (string filePath in arguments) { if (File.Exists(filePath)) { try { m_Img = new Bitmap(filePath); } catch { // Do something if error reading file as bitmap } } } } } } Jeff

                      A 1 Reply Last reply
                      0
                      • S Skippums

                        Just do this... static class Program { [STAThread] static void Main(string[] arguments) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(arguments)); } } public partial class Form1 : Form { Bitmap m_Img null; public Form1(string[] arguments) { InitializeComponent(); if (arguments != null) { foreach (string filePath in arguments) { if (File.Exists(filePath)) { try { m_Img = new Bitmap(filePath); } catch { // Do something if error reading file as bitmap } } } } } } Jeff

                        A Offline
                        A Offline
                        apoll
                        wrote on last edited by
                        #11

                        Hey Jeff, you are great, now it is just as i wanted. Thank you very much for your help and your time. Best regards Alexander

                        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