⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainwnd.cs

📁 将bmp文件转成16位彩色的C数组
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace bmp2c
{
	public partial class MainWnd : Form
	{
		public MainWnd()
		{
			InitializeComponent();
		}

		private void convertBtn_Click(object sender, EventArgs e)
		{
			Bitmap bmp = new Bitmap(bmpFileNameTxt.Text);
			widthTxt.Text = string.Format("{0}", bmp.Width);
			heightTxt.Text = string.Format("{0}", bmp.Height);
			StreamWriter s = new StreamWriter(cFileNameTxt.Text);
			s.WriteLine(@"const unsigned short ScreenBitmap[]={");
			s.Write("\t");
			using(bmp)
			{
				//Debug.Assert(bmp.Width == 800);
				//Debug.Assert(bmp.Height == 480);
				int ct = 0;
				for (int h = 0; h < bmp.Height; h++)
					for(int w = 0; w<bmp.Width;w++)
					{
						Color c = bmp.GetPixel(w, h);
						int n = ((c.R&0xf8) << 8) | ((c.G & 0xfc) << 3) | ((c.B & 0xf8) >> 3);
						s.Write("0x" + n.ToString("X4")+",");
						ct++;
						if(ct >= 80)
						{
							s.WriteLine("");
							ct = 0;
							s.Write("\t");
						}
					}
			}
			s.WriteLine("};");
			s.Close();
			s.Dispose();
			MessageBox.Show("转换完成");
		}

		private void readBtn_Click(object sender, EventArgs e)
		{
			if (widthTxt.Text == "" || heightTxt.Text == "")
			{
				MessageBox.Show("请指定图像的宽和高");
				return;
			}
			int w = int.Parse(widthTxt.Text);
			int h = int.Parse(heightTxt.Text);
			Bitmap bmp = new Bitmap(800, 480);
			int pixIndex = 0;
			using (StreamReader s = new StreamReader(cFileNameTxt.Text))
			{
				string line = s.ReadLine();
				while( (line = s.ReadLine()) != null)
				{
					string[] nums = line.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
					foreach(var n in nums)
					{
						int c = int.Parse(n.Substring(3),  System.Globalization.NumberStyles.HexNumber);
						bmp.SetPixel(pixIndex % 800, pixIndex / 800, 
							Color.FromArgb((0xf800 & c) >> 8, (0x07e0 & c) >> 3, (0x001f & c) <<3));
						pixIndex++;
						if (pixIndex >= 800 * 480)
							goto bmpOK;
					}
				}
			}
		bmpOK:
			pictureBox1.Image = bmp;
		}

		private void MainWnd_Load(object sender, EventArgs e)
		{
			
		}

		private void widthTxt_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!Char.IsDigit(e.KeyChar))
			{
				e.Handled = true;
				return;
			}

		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -