none
It is necessary to create a program in visual studio 2019 on c #. This program should increase and decrease the image from the computer, and then save the modified image in BMP format. RRS feed

  • Question

  • Hello! The other day I had a great idea to create a program for working with images. It is necessary to create a program in visual studio 2019 on c #. This program should increase and decrease the image from the computer, and then save the modified image in BMP format. Tell me, as a newbie, how can I make this work faster (arrange the program in the form of an application for windows). To make me happy and enjoy the success of programming !!!
    Sunday, April 28, 2019 7:26 PM

All replies

  • The other day I had a great idea to create a program for working with images. It is necessary to create a program in visual studio 2019 on c #. This program should increase and decrease the image from the computer, and then save the modified image in BMP format. Tell me, as a newbie, how can I make this work faster

    I assume that you are doing this just as a programming exercise? As there already
    are programs available that do this, including Windows Paint.

    >how can I make this work faster

    Faster than what?

    - Wayne


    • Edited by WayneAKing Monday, April 29, 2019 2:49 AM
    Monday, April 29, 2019 2:49 AM
  • What are you trying to learn?  You can write this program in about a dozen lines of code by using the system APIs to do the work, but in my opinion, writing image stretch and shrink routines is a very interesting mathematical exercise.  You would want to do some reading about stretch and shrink techniques; there are several ways to do it, each with its own tradeoffs.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, April 29, 2019 5:11 AM
  • Hello! I have already started making the application I need. I give a link to it below. I got the error there: "There is no overloaded method for" PictureBox1_Click ", which corresponds to the" EventHandler "delegate." I am writing a program on c # at a visual studio. Help me, please, get rid of this error and add a code with which you could save the resulting modified image in the BMP format. Link - https://drive.google.com/open?id=11xkDY_Ualm4p5GLMMM8o-jnqfSi9yrCf
    Monday, April 29, 2019 9:16 PM
  • Hello! I have already started making the application I need. I give a link to it below. I got the error there: "There is no overloaded method for" PictureBox1_Click ", which corresponds to the" EventHandler "delegate." I am writing a program on c # at a visual studio. Help me, please, get rid of this error and add a code with which you could save the resulting modified image in the BMP format. Link - https://drive.google.com/open?id=11xkDY_Ualm4p5GLMMM8o-jnqfSi9yrCf
    Monday, April 29, 2019 9:16 PM
  • Hello! I have already started making the application I need. I give a link to it below. I got the error there: "There is no overloaded method for" PictureBox1_Click ", which corresponds to the" EventHandler "delegate." I am writing a program on c # at a visual studio. Help me, please, get rid of this error and add a code with which you could save the resulting modified image in the BMP format. Link - https://drive.google.com/open?id=11xkDY_Ualm4p5GLMMM8o-jnqfSi9yrCf

    //Original code:
    
            private void PictureBox1_Click(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                Image img = new Bitmap(openFileDialog1.FileName);
                int width = img.Width;
                int height = img.Height;
    
    
                e. Graphics.InterpolationMode = InterpolationMode.Bilinear;
                e.Graphics.DrawImage(img, new Rectangle(0, 0, w1, h1), 0, 0, width, height, GraphicsUnit.Pixel);
    
            }
    
    

    I haven't tried these except to eliminate compile errors:

                // pictureBox1
                // 
                this.pictureBox1.Location = new System.Drawing.Point(59, 231);
                this.pictureBox1.Name = "pictureBox1";
                this.pictureBox1.Size = new System.Drawing.Size(295, 154);
                this.pictureBox1.TabIndex = 1;
                this.pictureBox1.TabStop = false;
                //System.EventHandler eventHandler = new System.EventHandler(this.PictureBox1_Click);
                //this.pictureBox1.Click += eventHandler;
                this.pictureBox1.Click += this.PictureBox1_Click;
    
    
    //Solution Attempt 1:
    
            //private void PictureBox1_Click(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            private void PictureBox1_Click(object sender, EventArgs e
            {
                Image img = new Bitmap(openFileDialog1.FileName);
                int width = img.Width;
                int height = img.Height;
    
                ((System.Drawing.Printing.PrintPageEventArgs)e).Graphics.InterpolationMode = InterpolationMode.Bilinear;
                ((System.Drawing.Printing.PrintPageEventArgs)e).Graphics.DrawImage(img, new Rectangle(0, 0, w1, h1), 0, 0, width, height, GraphicsUnit.Pixel);
            }
    
    
    
    //Solution Attempt 2:
    
            //private void PictureBox1_Click(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            private void PictureBox1_Click(object sender, EventArgs e)
            {
                Image img = new Bitmap(openFileDialog1.FileName);
                int width = img.Width;
                int height = img.Height;
    
                System.Drawing.Printing.PrintPageEventArgs ev =  (System.Drawing.Printing.PrintPageEventArgs)e;
    
                ev.Graphics.InterpolationMode = InterpolationMode.Bilinear;
                ev.Graphics.DrawImage(img, new Rectangle(0, 0, w1, h1), 0, 0, width, height, GraphicsUnit.Pixel);
            }
    

    - Wayne

    Tuesday, April 30, 2019 1:18 AM
  • See if this thread helps:

    Read picture box mouse coordinates on click
    https://stackoverflow.com/questions/18040945/read-picture-box-mouse-coordinates-on-click

    - Wayne

    Tuesday, April 30, 2019 5:21 AM
  • Thank you very much for the decision! I used your help! But I had new errors: when I open form1.cs, then the error "Variable 'eventHandler' or is not declared, or it was not assigned a value.". And the second mistake is when I launch the program, then pressing the button 3 on the button while the image is loaded (I pressed button 1) I get the error "System.NotImplementedException:" The method or operation is not implemented. "Please help in solving these errors. Link to project - https://drive.google.com/open?id=1IWZ6n19BeJCR1MeLC1cpz4GPWPfxQFch
    Tuesday, April 30, 2019 9:52 AM
  • Thanks for the link, but this is not what I would like.
    Tuesday, April 30, 2019 9:59 AM
  • The code you posted wouldn't get either of those errors.  Look, you are expected to be able to handle simple errors, and what you posted there were simple errors.

    You do have several problems in your code.  You are storing your scaling factor in a double, but every time you use it you convert it back to an integer.  Why?  Also, remember that Convert.ToInt16( 1.2 ) is always going to be one.  If you want to divide by a non-integral factor, you need to do the math first and THEN convert to int:   w1 = Convert.ToInt( w_temp / koef );

    However, you don't do anything with w1 and h1, so I can't guess what you're hoping this will do.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Wednesday, May 1, 2019 6:36 AM
  • Hello! I agree that mistakes are easy. Help me, please, fix them in my project, because I just started learning from С# on visual studio.
    • Edited by Homka78 Thursday, May 2, 2019 12:28 AM
    Thursday, May 2, 2019 12:28 AM
  • Hi   Homka78, 

    Thank you for posting here.

    From your description, I have made a test, you can use the following advice to avoid the errors:

    1.>> the error "Variable 'eventHandler' or is not declared, or it was not assigned a value."

    make a change in Form1.Designer.cs

    It will help you avoid the error: "Variable 'eventHandler' or is not declared, or it was not assigned a value."

     

    2.>>  the second mistake is when I launch the program, then pressing the button 3 on the button while the image is loaded (I pressed button 1) I get the error "System.NotImplementedException:" The method or operation is not implemented. "


    You use “throw new NotImplementedException()” to remind you to implement this function, So if you don’t want to get this Exception, you can comment it(or catch it).


    Besides, I have made a sample to save the image in the BMP format. You can refer it and modify it.
                
    Here’s the code:
         private void button12_Click(object sender, EventArgs e)
            {
                Bitmap bit = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                pictureBox1.DrawToBitmap(bit, pictureBox1.ClientRectangle);
                bit.Save(@"D:\Picture1.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                MessageBox.Show("Successful!");
                bit.Dispose();
            }
    
    Hope it will help you.


    Best Regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 2, 2019 3:54 AM
    Moderator
  • I obeyed you. But I, unfortunately, have new errors. I want the program to load the image, then increase and decrease it by coefficients 1.2; 3; 7; 21. To be able to watch the original image in the program, and in another window, the modified image (in size). And also, if the image does not fit to scroll. As for saving, I added a button, but I can’t check it, because when clicking on the expand key, the error "System.InvalidCastException:" Could not cast the object type "System.Windows.Forms.PaintEventArgs" to the type "System.Drawing.Printing.PrintPageEventArgs". "" Please help me !!! Link to my current project - https://drive.google.com/open?id=1uhroc1WyDCorLEHjMOiglS0QRsGA82iS
    Thursday, May 2, 2019 12:39 PM
  • I obeyed you. But I, unfortunately, have new errors. I want the program to load the image, then increase and decrease it by coefficients 1.2; 3; 7; 21. To be able to watch the original image in the program, and in another window, the modified image (in size). And also, if the image does not fit to scroll. As for saving, I added a button, but I can’t check it, because when clicking on the expand key, the error "System.InvalidCastException:" Could not cast the object type "System.Windows.Forms.PaintEventArgs" to the type "System.Drawing.Printing.PrintPageEventArgs". "" Please help me !!! Link to my current project - https://drive.google.com/open?id=1uhroc1WyDCorLEHjMOiglS0QRsGA82iS

    Hi  Homka78,


    You can change the code as below.

       private void Button1_Click(object sender, EventArgs e)
            {
                openFileDialog1.Filter = "BMP Picture |*.bmp";
                openFileDialog1.ShowDialog();
                pictureBox1.Hide();
                pictureBox2.Hide();
    
    
                pictureBox1.Paint += new PaintEventHandler(this.pictureBox1_Paint);
                pictureBox1.Refresh();
                Image img2 = new Bitmap(openFileDialog1.FileName);
                pictureBox2.Image = img2;
    
                w_temp = img2.Width;
                h_temp = img2.Height;
    
            }
    
            private void pictureBox1_Paint(object sender, PaintEventArgs e)
            {
                Image img = new Bitmap(openFileDialog1.FileName);
                int width = img.Width;
                int height = img.Height;
                e.Graphics.InterpolationMode = InterpolationMode.Bilinear;
                e.Graphics.DrawImage(img, new Rectangle(0, 0, w1, h1), 0, 0, width, height, GraphicsUnit.Pixel);
            }
    
            private void PictureBox1_Click(object sender, EventArgs  e)
            {
                pictureBox1.Refresh();
            }

    Besides, It would be appreciated if you could close the thread by marking helpful posts as an answer. This will help other members to find the solution quickly if they have faced the similar issue. If you have a new question you can start a new thread. Please don't ask several questions in the same thread.



    Best Regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 3, 2019 8:24 AM
    Moderator