圖片轉化為二進制

private void bttransform_Click(object sender, EventArgs e)
{
      this.lbpoint.Items.Clear();
      Bitmap bitmap1 = (Bitmap) this.originpicture.Image;
      GraphicsUnit unit1 = GraphicsUnit.Pixel;
      RectangleF ef1 = bitmap1.GetBounds(ref unit1);
      Point point1 = new Point((int) ef1.Width, (int) ef1.Height);
      for (int num2 = 0; num2 < point1.Y; num2++)
      {
            for (int num3 = 0; num3 < point1.X; num3++)
            {
                  int num1 = bitmap1.GetPixel(num3, num2).ToArgb();
                  this.lbpoint.Items.Add("(" + num2.ToString() + "," + num3.ToString() + ")" + num1.ToString("X"));
            }
            Application.DoEvents();
      }
      this.bttransform.Enabled = false;
}


private void btopen_Click(object sender, EventArgs e)
{
      this.ofdialog.Filter = "(*.*)|*.*|(*.bmp)|*.bmp";
      this.ofdialog.ShowDialog();
      this.originpicture.Image = Image.FromFile(this.ofdialog.FileName);
      this.bttransform.Enabled = true;
}

 
private void btConvert_Click(object sender, EventArgs e)
{
      int num1 = 0;
      long num2 = 0;
      this.tbcontents.Clear();
      this.tbcontents.AppendText("^XA\r\n~DGR:ZONE.GRF,");
      Bitmap bitmap1 = (Bitmap) this.originpicture.Image;
      int num4 = ((bitmap1.Size.Width / 8) + (((bitmap1.Size.Width % 8) == 0) ? 0 : 1)) * bitmap1.Size.Height;
      int num5 = (bitmap1.Size.Width / 8) + (((bitmap1.Size.Width % 8) == 0) ? 0 : 1);
      this.tbcontents.AppendText(num4.ToString().PadLeft(5, '0') + "," + num5.ToString().PadLeft(3, '0') + ",\n");
      Bitmap bitmap2 = new Bitmap(bitmap1.Size.Width, bitmap1.Size.Height);
      for (int num6 = 0; num6 < bitmap1.Size.Height; num6++)
      {
            for (int num7 = 0; num7 < bitmap1.Size.Width; num7++)
            {
                  num1 *= 2;
                  long num3 = bitmap1.GetPixel(num7, num6).ToArgb();
                  string text1 = num3.ToString("X");
                  if (text1.Substring(text1.Length - 6, 6).CompareTo("BBBBBB") < 0)
                  {
                        bitmap2.SetPixel(num7, num6, bitmap1.GetPixel(num7, num6));
                        num1++;
                  }
                  num2++;
                  if ((num7 == (bitmap1.Size.Width - 1)) && (num2 < 8))
                  {
                        num1 *= 2 ^ (8 - ((int) num2));
                        this.tbcontents.AppendText(num1.ToString("X").PadLeft(2, '0'));
                        num1 = 0;
                        num2 = 0;
                  }
                  if (num2 >= 8)
                  {
                        this.tbcontents.AppendText(num1.ToString("X").PadLeft(2, '0'));
                        num1 = 0;
                        num2 = 0;
                  }
            }
            this.tbcontents.AppendText("\r\n");
      }
      this.tbcontents.AppendText("^LH0,0\r\n^FO5,60^XGR:ZONE.GRF^FS\r\n^XZ\r\n");
      this.pictureBox1.Image = bitmap2;
}


[DllImport("gdi32")]
public static extern long GetPixel(IntPtr handle, long x, long y);
 
[DllImport("gdi32")]
public static extern long SetPixel(IntPtr handle, long x, long y, long crColor);

<HEAD><style media="print">
   .Noprint { DISPLAY: none; page-break-afger: always }
   .pageNext { page-break-afger: always }
  </style>
 </HEAD>
 <BODY>
  <form id="Form1" method="post" runat="server">
   <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" border="0" class="noprint">
    <TR>
     <TD>
      <object id ="factory" style="display:none" viewastext classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" ></object> 
      <object id="WebBrowser" height="0" width="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" VIEWASTEXT>
      </object>
      <input type="button" value="打    印" onclick="document.all.WebBrowser.ExecWB(6,1)">
      <input type="button" value="頁面設置" onclick="document.all.WebBrowser.ExecWB(8,1)">
      <input type="button" value="打印預覽" onclick="document.all.WebBrowser.ExecWB(7,1)">
     </TD>
    </TR>
   </TABLE>

posted on 2007-02-27 14:45  KenL  阅读(756)  评论(1)    收藏  举报

导航