C#unsafe不安全指针的使用

2025-11-12 05:22:03

1、打开或创建一个新的WinFrom工程,在解决方案资源管理器中选中当前工程项目,鼠标右击,弹出右键菜单,移动鼠标点击【属性】,弹出属性界面,选择【生成】项,勾选中【允许不安全代码】

C#unsafe不安全指针的使用

2、保存工程项目,关闭属性页面。选中Form1.cs页面,增加帮助菜单项和Label一个,用于提示软件功能和软件执行进度。设置Form1窗口允许拖放功能及拖放事件。界面设计代码如下:

 partial class Form1

    {

        /// <summary>

        /// 必需的设计器变量。

        /// </summary>

        private System.ComponentModel.IContainer components = null;

        /// <summary>

        /// 清理所有正在使用的资源。

        /// </summary>

        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }

        #region Windows 窗体设计器生成的代码

        /// <summary>

        /// 设计器支持所需的方法 - 不要

        /// 使用代码编辑器修改此方法的内容。

        /// </summary>

        private void InitializeComponent()

        {

            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));

            this.label1 = new System.Windows.Forms.Label();

            this.menuStrip1 = new System.Windows.Forms.MenuStrip();

            this.帮助ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

            this.menuStrip1.SuspendLayout();

            this.SuspendLayout();

            // 

            // label1

            // 

            this.label1.AutoSize = true;

            this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));

            this.label1.Location = new System.Drawing.Point(86, 98);

            this.label1.Name = "label1";

            this.label1.Size = new System.Drawing.Size(56, 16);

            this.label1.TabIndex = 0;

            this.label1.Text = "label1";

            // 

            // menuStrip1

            // 

            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {

            this.帮助ToolStripMenuItem});

            this.menuStrip1.Location = new System.Drawing.Point(0, 0);

            this.menuStrip1.Name = "menuStrip1";

            this.menuStrip1.Size = new System.Drawing.Size(446, 25);

            this.menuStrip1.TabIndex = 1;

            this.menuStrip1.Text = "menuStrip1";

            // 

            // 帮助ToolStripMenuItem

            // 

            this.帮助ToolStripMenuItem.Name = "帮助ToolStripMenuItem";

            this.帮助ToolStripMenuItem.Size = new System.Drawing.Size(44, 21);

            this.帮助ToolStripMenuItem.Text = "帮助";

            this.帮助ToolStripMenuItem.Click += new System.EventHandler(this.帮助ToolStripMenuItem_Click);

            // 

            // Form1

            // 

            this.AllowDrop = true;

            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

            this.ClientSize = new System.Drawing.Size(446, 236);

            this.Controls.Add(this.label1);

            this.Controls.Add(this.menuStrip1);

            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

            this.MainMenuStrip = this.menuStrip1;

            this.Name = "Form1";

            this.Text = "看到开发版Raw12转RAW8";

            this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);

            this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);

            this.menuStrip1.ResumeLayout(false);

            this.menuStrip1.PerformLayout();

            this.ResumeLayout(false);

            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;

        private System.Windows.Forms.MenuStrip menuStrip1;

        private System.Windows.Forms.ToolStripMenuItem 帮助ToolStripMenuItem;

    }

C#unsafe不安全指针的使用

3、为From1添加代码:

    public partial class Form1 : Form

    {

        public unsafe Form1()

        {

            InitializeComponent();

            this.label1.Text = "将需要转Raw12文件拖动到当前窗口";

        }

        private unsafe void Form1_DragDrop(object sender, DragEventArgs e)

        {

            string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);

            dynamic  path = s[0];

            System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open);

            byte[] bs = new byte[fs.Length];

            byte[] bs2 = new byte[fs.Length / 2];

            fs.Read(bs, 0, bs.Length);

            fs.Close();

            fixed (byte* b = bs, b2 = bs2)

            {

                ushort* u = (ushort*)b;

                byte* p = b2;

                for (int i = 0; i < bs2.Length; i++)

                {

                    int u2 = *u - 200;

                    if (u2 <= 0)

                    {

                        u2 = 0;

                    }

                    *p = (byte)(u2 / 16);

                    p++;

                    u++;

                }

            }

            System.IO.FileStream fs2 = new System.IO.FileStream(path + "[8]" + ".raw", System.IO.FileMode.Create);

            fs2.Write(bs2, 0, bs2.Length);

            fs2.Flush();

            fs2.Close();

            this.label1.Text = System.IO.Path.GetFileName(path) + " 转8位完成";

        }

        private void Form1_DragEnter(object sender, DragEventArgs e)

        {

            if (e.Data.GetDataPresent(DataFormats.FileDrop))

                e.Effect= DragDropEffects.All;

            else

                e.Effect = DragDropEffects.None;

        }

        private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            MessageBox.Show("将需要转的文件拖动到窗口\r\n该软件支持12位RawData数据转换成8位RawData\r\n\r\n");

        }

    }

C#unsafe不安全指针的使用

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢