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

  • 相关阅读:
    再谈TextField
    IOS-TextField知多少
    leftBarButtonItems
    LeftBarButtonItems,定制导航栏返回按钮
    Apple Mach-O Linker (id) Error "_OBJC_CLASS...错误解决办法 Apple Mach-O Linker (id) Error "_OBJC_CLASS...错误解决办法
    Unrecognized Selector Sent to Instance问题之诱敌深入关门打狗解决办法
    UNRECOGNIZED SELECTOR SENT TO INSTANCE 问题快速定位的方法
    Present ViewController,模态详解
    UILABEL AUTOLAYOUT自动换行 版本区别
    iOS自动布局解决警告Automatic Preferred Max Layout Width is not available on iOS versions prior to 8.0
  • 原文地址:https://www.cnblogs.com/hdjjun/p/1223841.html
Copyright © 2011-2022 走看看