Как работать с отдельными пикселями на изображении - C#

Узнай цену своей работы

Формулировка задачи:

Плиз помогите как работать с отдельными пикселями на изображении. Если точнее то я использую сетпиксель и гет пиксель, а при сохранении изменённой картинки картинка сохраняется неизменённой подскажите в чём проблема???
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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd1 = new OpenFileDialog();
            if (ofd1.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Image = Image.FromFile(ofd1.FileName);
                textBox1.Text = ofd1.FileName;
            }
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            int dlina = textBox2.Text.Length*8;
            int dlinapixel = dlina / 3 + 1;
            int dlinakey = textBox3.Text.Length * 8;
            int dlinakeypixel = dlinakey / 3 + 1;
          //  textBox4.Text += "dlina=" + dlina.ToString() + (char)13 + (char)10;
          //  textBox4.Text += "dlinapixel=" + dlinapixel.ToString() + (char)13 + (char)10;
          //  textBox4.Text += "======================================" +  (char)13 + (char)10;
 
            byte[] messagebyte = Encoding.ASCII.GetBytes(textBox2.Text);
           // for(int i = 0; i<messagebyte.Length;i++)
           //     textBox4.Text +="messagebyte["+i+"]= "+ messagebyte[i] + (char)13 + (char)10;
           // textBox4.Text += "======================================" + (char)13 + (char)10;
            int value;
            string message = "";
            for (int i = 0; i < messagebyte.Length; i++)
            {
                value = Convert.ToInt16(messagebyte[i]);
                message += Convert.ToString(value, 2)+ " ";
            }
           // textBox4.Text += "message= " + message + (char)13 + (char)10;
            //textBox4.Text += "======================================" + (char)13 + (char)10;
            message = message.Trim();
            string[] message2 = message.Split(' ');
            for (int i = 0; i < message2.Length; i++)
                while (message2[i].Length != 8)
                    message2[i] = "0" + message2[i];
           // for (int i = 0; i < message2.Length; i++)
           //     textBox4.Text += "message2[" + i + "]= " + message2[i] + (char)13 + (char)10;
           // textBox4.Text += "======================================" + (char)13 + (char)10;

            Color pixelcolor;
            Bitmap bmp1 = new Bitmap(pictureBox1.Image);
            string[] rmessage = new string[dlinapixel];
            string[] gmessage = new string[dlinapixel];
            string[] bmessage = new string[dlinapixel];
            string[] rkey = new string[dlinakeypixel];
            string[] gkey = new string[dlinakeypixel];
            string[] bkey = new string[dlinakeypixel];
            byte colorr, colorg, colorb, keycolorr, keycolorg, keycolorb;
 
            for (int i = 0; i < dlinapixel; i++)
            {
                pixelcolor = bmp1.GetPixel(i, 1);
                colorr = pixelcolor.R;
                colorg = pixelcolor.G;
                colorb = pixelcolor.B;
                rmessage[i] = Convert.ToString(colorr, 2).ToString();
                gmessage[i] = Convert.ToString(colorg, 2).ToString();
                bmessage[i] = Convert.ToString(colorb, 2).ToString();
                while (rmessage[i].Length != 8)
                {
                    rmessage[i] = "0" + rmessage[i];
                }
                while (gmessage[i].Length != 8)
                {
                    gmessage[i] = "0" + gmessage[i];
                }
                while (bmessage[i].Length != 8)
                {
                    bmessage[i] = "0" + bmessage[i];
                }
            }
 
            for (int i = 0; i < dlinapixel; i++)
            {
                textBox4.Text += "rmessage[" + i + "]" + " = " + rmessage[i] + (char)13 + (char)10;
                textBox4.Text += "gmessage[" + i + "]" + " = " + gmessage[i] + (char)13 + (char)10;
                textBox4.Text += "bmessage[" + i + "]" + " = " + bmessage[i] + (char)13 + (char)10;
                textBox4.Text += "============================" + (char)13 + (char)10;
            }

            for (int i = 0; i < dlinakeypixel; i++)
            {
                pixelcolor = bmp1.GetPixel(i, 0);
                keycolorr = pixelcolor.R;
                keycolorg = pixelcolor.G;
                keycolorb = pixelcolor.B;
                rkey[i] = Convert.ToString(keycolorr, 2).ToString();
                gkey[i] = Convert.ToString(keycolorg, 2).ToString();
                bkey[i] = Convert.ToString(keycolorb, 2).ToString();
                while (rkey[i].Length != 8)
                {
                    rkey[i] = "0" + rkey[i];
                }
                while (gkey[i].Length != 8)
                {
                    gkey[i] = "0" + gkey[i];
                }
                while (bkey[i].Length != 8)
                {
                    bkey[i] = "0" + bkey[i];
                }
                
            }
 
  /*          for (int i = 0; i < dlinakeypixel; i++)
            {
                textBox4.Text += "rkey[" + i + "]" + " = " + rkey[i] + (char)13 + (char)10;
                textBox4.Text += "gkey[" + i + "]" + " = " + gkey[i] + (char)13 + (char)10;
                textBox4.Text += "bkey[" + i + "]" + " = " + bkey[i] + (char)13 + (char)10;
                textBox4.Text += "============================" + (char)13 + (char)10;
            }
            */
 
            string keyfull = "";
            byte[] key = Encoding.ASCII.GetBytes(textBox3.Text);
            for (int i = 0; i < key.Length; i++)
                keyfull += Convert.ToString(key[i], 2).ToString() + " ";
            keyfull = keyfull.Trim();
            string[] key2 = keyfull.Split(' ');
            for (int j = 0; j < key2.Length; j++)
            {
                while (key2[j].Length != 8)
                {
                    key2[j] = "0" + key2[j];
                }
            //    textBox4.Text += "key2[" + j + "]" + " = " + key2[j] + (char)13 + (char)10;
            }
 
          //  textBox4.Text += "============================" + (char)13 + (char)10;

            string fullmessage = "";
            string fullkey = "";
            string sub = "";

      for (int i = 0; i < key2.Length; i++)
                fullkey += key2[i];
 
        //    textBox6.Text = fullkey;
 
            int k = 0;
            while (fullkey.Length>0)
            {
                rkey[k] = rkey[k].Remove(7, 1);
                sub = fullkey.Substring(0, 1);
                fullkey = fullkey.Remove(0, 1);
                rkey[k] += sub;
                if (fullkey.Length == 0)
                    break;
 
                gkey[k] = gkey[k].Remove(7, 1);
                sub = fullkey.Substring(0, 1);
                fullkey = fullkey.Remove(0, 1);
                gkey[k] += sub;
                if (fullkey.Length == 0)
                    break;
 
                bkey[k] = bkey[k].Remove(7, 1);
                sub = fullkey.Substring(0, 1);
                fullkey = fullkey.Remove(0, 1);
                bkey[k] += sub;
                if (fullkey.Length == 0)
                    break;
                k++;
            }
 
     /*       for (int i = 0; i < dlinakeypixel; i++)
            {
                textBox5.Text += "rkey[" + i + "]" + " = " + rkey[i] + (char)13 + (char)10;
                textBox5.Text += "gkey[" + i + "]" + " = " + gkey[i] + (char)13 + (char)10;
                textBox5.Text += "bkey[" + i + "]" + " = " + bkey[i] + (char)13 + (char)10;
                textBox5.Text += "============================" + (char)13 + (char)10;
            }
 
            */

            for (int i = 0; i < message2.Length; i++)
                fullmessage += message2[i];
        //    textBox6.Text = fullmessage;
 
            k = 0;
            while (fullmessage.Length > 0)
            {
                rmessage[k] = rmessage[k].Remove(7, 1);
                sub = fullmessage.Substring(0, 1);
                fullmessage = fullmessage.Remove(0, 1);
                rmessage[k] += sub;
                if (fullmessage.Length == 0)
                    break;
 
                gmessage[k] = gmessage[k].Remove(7, 1);
                sub = fullmessage.Substring(0, 1);
                fullmessage = fullmessage.Remove(0, 1);
                gmessage[k] += sub;
                if (fullmessage.Length == 0)
                    break;
 
                bmessage[k] = bmessage[k].Remove(7, 1);
                sub = fullmessage.Substring(0, 1);
                fullmessage = fullmessage.Remove(0, 1);
                bmessage[k] += sub;
                if (fullmessage.Length == 0)
                    break;
                k++;
            }
 
      /*      for (int i = 0; i < dlinapixel; i++)
            {
                textBox5.Text += "rmessage[" + i + "]" + " = " + rmessage[i] + (char)13 + (char)10;
                textBox5.Text += "gmessage[" + i + "]" + " = " + gmessage[i] + (char)13 + (char)10;
                textBox5.Text += "bmessage[" + i + "]" + " = " + bmessage[i] + (char)13 + (char)10;
                textBox5.Text += "============================" + (char)13 + (char)10;
            }
            */
 
            //устанавливаю цвет пикселя
            bmp1.SetPixel(1, 0, Color.FromArgb(255,0,0,0));
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = Image.FromFile(textBox1.Text);
            Bitmap bmp1 = new Bitmap(pictureBox1.Image);
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.DefaultExt = "bmp";
            sfd.Filter = "BitMap picture (*.bmp)|*.bmp";
            if (sfd.ShowDialog() == DialogResult.OK)
                bmp1.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
        }
    }
}

Решение задачи: «Как работать с отдельными пикселями на изображении»

textual
Листинг программы
 bmp1.SetPixel(1, 0, Color.FromArgb(255,0,0,0));

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 3.909 из 5
Похожие ответы