Search code examples
c#imagebitmappixelbitmapimage

Can't create a new bitmap from copied pixels


I have a picture where i want store a message (by changing last significant bit of each pixel needed). I'm using Bitmap to load the pixels into a List.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private string oEcrypt;
    private string oDecrypt;
    private List<ARGB> toCipherList = new List<ARGB>();
    private int Width;
    private int Height;
    private void button1_Click(object sender, EventArgs e)
    {
        // here i'm editing toCipherList
        Bitmap img = new Bitmap(Width, Height);
        for (int i = 0; i < img.Width; i++)
        {
            for (int j = 0; j < img.Height; j++)
            {
                Color pixel = Color.FromArgb(toCipherList[i].A, toCipherList[i].R, toCipherList[i].G, toCipherList[i].B);
                img.SetPixel(i, j, pixel);
            }
        }
        img.Save("output.jpg");
    }

    private void button3_Click(object sender, EventArgs e)
    {
        openFileDialog1.ShowDialog();
        oEcrypt = openFileDialog1.FileName;
        textBox3.Text = oEcrypt;
        Bitmap img = new Bitmap(oEcrypt);
        Width = img.Width;
        Height = img.Height;
        for (int i = 0; i < img.Width; i++)
        {
            for (int j = 0; j < img.Height; j++)
            {
                Color pixel = img.GetPixel(i, j);
                byte A = pixel.A;
                byte R = pixel.R;
                byte G = pixel.G;
                byte B = pixel.B;
                ARGB rGB = new ARGB(A, R, G, B);
                toCipherList.Add(rGB);
            }
        }
    }
    public class ARGB
    {
         public byte A { get; set; }
         public byte R { get; set; }
         public byte G { get; set; }
         public byte B { get; set; }

    public ARGB(byte a, byte r, byte g, byte b)
    {
        this.A = a;
        this.R = r;
        this.G = g;
        this.B = b;
    }
}

The thing is, even if I don't change the pixels, just make a new bitmap with pixels which I got from the original image, I get the output image with some random vertical lines. I checked the values stored in List, they are the same as the original image when it was loaded. Do i need to copy more information? I was trying GetCopyOf method from here, but it didn't work for me, or I did something wrong.


Solution

  • There are many many many better approaches here. However your problem is with the List. In short, you are not reading each element. Try this

    var index = 0;
    ...
    
    // loops here
    Color pixel = Color.FromArgb(toCipherList[index].A, toCipherList[index].R, toCipherList[index].G, toCipherList[index].B);
    index++;
    

    Some Tips,

    1. Use LockBits its lots faster
    2. Use 32bpp then your pixels can be stored as ints
    3. Don't bother with your ARGB classes see above