资源简介
网上有很多BMP转Texture2d的代码,但是大多都不能用!!!这个脚本是通过读取BMP文件的字节流解析协议直接将BMP从Byte[]解析出来的算法,是解析BMP的算法,根据这个算法可以在所有平台上解析.BMP格式的图片。
代码片段和文件信息
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
public RawImage outputImg;
byte[] file = null;
private void Start()
{
Texture2D t = ReadBmp();
outputImg.rectTransform.sizeDelta = new Vector2(t.width t.height);
outputImg.texture = t;
}
public Texture2D ReadBmp()
{
file = File.ReadAllBytes(“请将该路径修改为自己BMP图片的绝对路径“);
tagBITMAPFILEHEADER file_header = new tagBITMAPFILEHEADER();
file_header.bfType = System.Text.Encoding.Default.GetString(file 0 2);
file_header.bfSize = _4ByteToInt(0x0002);
file_header.bfReserved1 = _2ByteToInt(0x0006);
file_header.bfReserved2 = _2ByteToInt(0x0008);
file_header.bfOffBits = _4ByteToInt(0x000A);
tagBMP_INFOHEADER info_header = new tagBMP_INFOHEADER();
info_header.biSize = _4ByteToInt(0x000E);
info_header.biWidth = _4ByteToInt(0x0012);
info_header.biHeight = _4ByteToInt(0x0016);
info_header.biPlanes = _2ByteToInt(0x001A);
info_header.biBitCount = _2ByteToInt(0x001C);
info_header.biCompression = _4ByteToInt(0x001E);
info_header.biSizeImage = _4ByteToInt(0x0022);
info_header.biXPelsPerMeter = _4ByteToInt(0x0026);
info_header.biYPelsPerMeter = _4ByteToInt(0x002A);
info_header.biClrUsed = _4ByteToInt(0x002E);
info_header.biClrImportant = _4ByteToInt(0x0032);
if (info_header.biBitCount != 24)
{
Debug.LogError(“本範例只能讀取 24 bit 寬度的圖像。:“ + info_header.biBitCount);
return null;
}
Texture2D t = new Texture2D(info_header.biWidth info_header.biHeight);
// skip : 微軟規定圖片寬度 / 4 必須 等於 0
// 如果寬度無法整除 4 ,那麼必須要補 0
int skip = 0;
if (t.width * 3 % 4 != 0)
{
skip = 4 - (t.width * 3 % 4);
}
int i = 0;
for (int y = 0; y < t.height; y++)
{
for (int x = 0; x < t.width; x++)
相关资源
- unity+Socket+KCP+Protobuf的Unity局域网联机
- AssetStudio V0.16.0 源代码 使用VS2019
- Unity摄像机分辨率自适应设计尺寸脚本
- unity 2d-extras-master插件
- polyvav2D-1.5.2_BehaviorDesigner.unitypackage
- unity读取Excel
- 软键盘(Package_KeyBoard.unitypackage)
- unity5圣典中文API手册v0.02.chm
- Unity 房间游览
- 百塞尔曲线粒子插件(.unitypackage)
- 皮带传送、齿轮unity
- unity3d脚本学习教程
- Unity日历插件
- Unity5实战:使用C#和Unity开发多平台游
- Unity5.0切水果游戏C#
- Unity2D小游戏——炸弹人类似qq堂的小
- 俄罗斯方框UNITY项目案例包含代码
- unity 键鼠控制摄像机移动 脚本
- Unity通过TCP接收服务端数据
- Unity和C#实现TCP网络聊天
- 愤怒的小鸟unity3d279320
- Unity in Action: Multiplatform Game Developmen
- unity开发贪吃蛇
- snake vs blocks 素材和源码
- PureMVC for Unity3d Demo
- unity编辑器源代码.zip
- unity迷你太空射击游戏源码
- System.web源码C#
- Unity3dMYO肌电臂环二次开发例程(保证
- Unity 3D Paint in 3D 最新版(1.56)
评论
共有 条评论