zoukankan      html  css  js  c++  java
  • 隐写术(将信息写入图片中)

     public partial class Form1 : Form
        {
            private Bitmap bitmapOriginal;
            private Bitmap bitmapModified;
            public Form1()
            {
                InitializeComponent();
                this.Paint += new System.Windows.Forms.PaintEventHandler(this.MyPaint);
                try
                {
                    //load original bitmap from a file
                    bitmapOriginal = (Bitmap)Bitmap.FromFile(
                        @"..\..\katie_plaintext.jpg");

                    //center to screen
                    this.CenterToScreen();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(
                        "Error loading image. " +
                        ex.Message);
                }
            }
            private void MyPaint(
                object sender,
                System.Windows.Forms.PaintEventArgs e)
            {
                try
                {
                    //get Graphics object for painting original
                    Graphics gPanelOriginal =
                        Graphics.FromHwnd(
                            panelOriginalImage.Handle);

                    //draw original bitmap into panel
                    gPanelOriginal.DrawImage(
                        bitmapOriginal, new Point(0, 0));

                    //return if there is no modified image yet
                    if (bitmapModified == null)
                        return;

                    //get Graphics object for painting modified
                    Graphics gPanelModified =
                        Graphics.FromHwnd(
                            panelModifiedImage.Handle);

                    //draw modified bitmap into panel
                    gPanelModified.DrawImage(
                        bitmapModified, new Point(0, 0));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(
                        "Error drawing image." +
                        ex.Message);
                    this.Close();
                }
            }
            private void button1_Click(object sender, EventArgs e)//写入
            {
                try
                {
                    //show wait cursor
                    this.Cursor = Cursors.WaitCursor;

                    //start off with copy of original image
                    bitmapModified = new Bitmap(
                        bitmapOriginal,
                        bitmapOriginal.Width,
                        bitmapOriginal.Height);

                    //get original message to be hidden
                    int numberbytes =
                        (byte)textBoxOriginalMessage.Text.Length;
                    byte[] bytesOriginal = new byte[numberbytes + 1];
                    bytesOriginal[0] = (byte)numberbytes;
                    Encoding.UTF8.GetBytes(
                        textBoxOriginalMessage.Text,
                        0,
                        textBoxOriginalMessage.Text.Length,
                        bytesOriginal,
                        1);

                    //set bits 1, 2, 3 of byte into LSB red
                    //set bits 4, 5, 6 of byte into LSB green
                    //set bits 7 and 8 of byte into LSB blue
                    int byteCount = 0;
                    for (int i = 0; i < bitmapOriginal.Width; i++)
                    {
                        for (int j = 0; j < bitmapOriginal.Height; j++)
                        {
                            if (bytesOriginal.Length == byteCount)
                                return;

                            Color clrPixelOriginal =
                                bitmapOriginal.GetPixel(i, j);
                            byte r =
                                (byte)((clrPixelOriginal.R & ~0x7) |
                                (bytesOriginal[byteCount] >> 0) & 0x7);
                            byte g =
                                (byte)((clrPixelOriginal.G & ~0x7) |
                                (bytesOriginal[byteCount] >> 3) & 0x7);
                            byte b =
                                (byte)((clrPixelOriginal.B & ~0x3) |
                                (bytesOriginal[byteCount] >> 6) & 0x3);
                            byteCount++;

                            //set pixel to modified color
                            bitmapModified.SetPixel(
                                i, j, Color.FromArgb(r, g, b));
                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(
                        "Error hiding message." +
                        ex.Message);
                }
                finally
                {
                    //show normal cursor
                    this.Cursor = Cursors.Arrow;

                    //repaint
                    Invalidate();
                }
            }

            private void button2_Click(object sender, EventArgs e)//读取
            {
                //get bytes of message from modified image
                byte[] bytesExtracted = new byte[256 + 1];
                try
                {
                    //show wait cursor, can be time-consuming
                    this.Cursor = Cursors.WaitCursor;

                    //get bits 1, 2, 3 of byte from LSB red
                    //get bits 4, 5, 6 of byte from LSB green
                    //get bits 7 and 8 of byte from LSB blue
                    int byteCount = 0;
                    for (int i = 0; i < bitmapModified.Width; i++)
                    {
                        for (int j = 0; j < bitmapModified.Height; j++)
                        {
                            if (bytesExtracted.Length == byteCount)
                                return;

                            Color clrPixelModified =
                                bitmapModified.GetPixel(i, j);
                            byte bits123 =
                                (byte)((clrPixelModified.R & 0x7) << 0);
                            byte bits456 = (
                                byte)((clrPixelModified.G & 0x7) << 3);
                            byte bits78 = (
                                byte)((clrPixelModified.B & 0x3) << 6);

                            bytesExtracted[byteCount] =
                                (byte)(bits78 | bits456 | bits123);
                            byteCount++;
                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(
                        "Error extracting message." +
                        ex.Message);
                }
                finally
                {
                    //show normal cursor
                    this.Cursor = Cursors.Arrow;

                    //get number of bytes from start of array
                    int numberbytes = bytesExtracted[0];

                    //get remaining bytes in array into string
                    textBoxExtractedlMessage.Text =
                        Encoding.UTF8.GetString(
                        bytesExtracted,
                        1,
                        numberbytes);
                }  
            }
        }


    demo:https://files.cnblogs.com/hdjjun/windowsapplication1.rar

  • 相关阅读:
    DNA 序列翻译
    python3 练习题100例 (二十九)猴子吃桃问题
    python3 练习题100例 (二十八)打印一定范围内的素数
    python3 练习题100例 (二十七)列表元素改写
    python3 练习题100例 (二十六)回文数判断
    python3 练习题100例 (二十五)打印一个n层金字塔
    python3 练习题100例 (二十四)打印完数
    python3 练习题100例 (二十三)与7相关的数
    python3 练习题100例 (二十二)输入两个字符串,输出两个字符串集合的并集
    python3 练习题100例 (二十一)打印一定范围内的水仙花数
  • 原文地址:https://www.cnblogs.com/hdjjun/p/1223841.html
Copyright © 2011-2022 走看看