E
E
Evgeny Petryaev2020-10-14 15:46:44
C++ / C#
Evgeny Petryaev, 2020-10-14 15:46:44

Why doesn't the color in the picture change?

I change the color of the scrollbars of the picture in the picturebox, but no effect?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            hScrollBar4.Value = 0;
            hScrollBar1.Value = 0;
            hScrollBar2.Value = 0;
            hScrollBar3.Value = 0;
            hScrollBar4.Maximum = 255;
            hScrollBar1.Maximum = 255;
            hScrollBar2.Maximum = 255;
            hScrollBar3.Maximum = 255;
        }
        private Bitmap MyImage;
        
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
                return;
            // получаем выбранный файл
            string filename = openFileDialog1.FileName;
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            MyImage = new Bitmap(filename);
            pictureBox1.Image = (Image)MyImage;
        }
        private void Change()
        {
            for (int i = 0; i < pictureBox1.Image.Width; i++)
                for (int j = 0; j < pictureBox1.Image.Height ; j++)
                    ((Bitmap)pictureBox1.Image).SetPixel(i, j, Color.FromArgb(hScrollBar4.Value, hScrollBar1.Value, hScrollBar2.Value, hScrollBar3.Value));
            pictureBox1.Update();
        }
        private void label1_Click(object sender, EventArgs e)
        {
            //Change();
        }

        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {

            Change();
        }

        private void hScrollBar2_Scroll(object sender, ScrollEventArgs e)
        {

            Change();
        }

        private void hScrollBar3_Scroll(object sender, ScrollEventArgs e)
        {
            Change();

        }

        private void hScrollBar4_Scroll(object sender, ScrollEventArgs e)
        {
            Change();

        }

        private void hScrollBar1_ValueChanged(object sender, EventArgs e)
        {
            Change();

        }

        private void hScrollBar2_ValueChanged(object sender, EventArgs e)
        {
            Change();

        }

        private void hScrollBar3_ValueChanged(object sender, EventArgs e)
        {
            Change();

        }

        private void hScrollBar4_ValueChanged(object sender, EventArgs e)
        {

            Change();
        }

        private void hScrollBar4_MouseCaptureChanged(object sender, EventArgs e)
        {

            Change();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
cicatrix, 2020-10-14
@Gremlin92

Firstly, it will all work terribly slowly if the picture is at least somewhat large. In addition, when scrolling, the program will "freeze".
Secondly, here:

((Bitmap)pictureBox1.Image).SetPixel(i, j, Color.FromArgb(hScrollBar4.Value, hScrollBar1.Value, hScrollBar2.Value, hScrollBar3.Value))

You are changing the copy of the picture, not the picture directly from the Picturebox. Now we need to re-assign the changed picture to the Picturebox.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question