C++ 转为 C# 请高手帮帮我!小弟在此谢过了

jim-single 2012-05-04 05:11:06
现在我有段 C++ 代码? 可以我想用C# 把他写出来 ,不知道这些, 希望大家帮我弄弄, 谢谢,

#define BFT_BITMAP 0x4d42 /* 'BMP' */
#define BI_RGB 0L
#define BI_RLE8 1L
#define BI_RLE4 2L
#define BI_BITFIELDS 3L

// 读取bmp格式文件,并转换成灰度图,目前只支持彩色和灰度图像
int ReadBmp(char * FileName, unsigned char * pImg, int *width, int *height)
{
FILE *hFile;
unsigned short bfType,biBitCount;
unsigned int biSize,dwCompression, biClrUsed, bfOffBits;
int biWidth, biHeight;
unsigned int dwEffWidth;
int i;

hFile=fopen(FileName,"rb");
if (hFile == NULL) return 0;

fread(&bfType,2,1,hFile);
if (bfType != BFT_BITMAP) { //do we have a RC HEADER?
printf("Not bmp File!\n");
return 0;
}
fseek(hFile,10,SEEK_SET);
fread(&bfOffBits,4,1,hFile);
fread(&biSize,4,1,hFile);
if (biSize!=40) {
printf("Not common BITMAPINFOHEADER, BITMAPCOREHEADER=12!\n");
return 0;
}
fread(&biWidth,4,1,hFile);
fread(&biHeight,4,1,hFile);
fseek(hFile,2,SEEK_CUR); // 跳过 biPlanes
fread(&biBitCount,2,1,hFile);
fread(&dwCompression,4,1,hFile);
fseek(hFile,12,SEEK_CUR); // 跳过 biPlanes
fread(&biClrUsed,4,1,hFile);

if (dwCompression!=BI_RGB) {
printf("Not supported Compression!\n");
fclose(hFile);
return 0;
}

if (biBitCount!=24) {
printf("only support 24bit color!\n");
fclose(hFile);
return 0;
}

// if (biClrUsed!=0) {
// printf("Palette not supported, Colors=%d\n",biClrUsed);
// return false;
// }

/*
pImg= (unsigned char *) malloc (biHeight*biWidth*3);

if (pImg==NULL) {
printf("no memory when alloc image\n");
fclose(hFile);
return false;
}
*/
dwEffWidth = ((((biBitCount * biWidth) + 31) / 32) * 4);

*width = biWidth;
*height= biHeight;

printf("%d,%d\n",biWidth,biHeight);

// 定位图像数据
fseek(hFile,bfOffBits,SEEK_SET);
for(i=0;i<biHeight;i++)
{
fread(pImg+(biHeight-1-i)*biWidth*3, dwEffWidth,1,hFile); // read in the pixels
}

fclose(hFile);
return 1;
}
...全文
156 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jim-single 2012-05-07
  • 打赏
  • 举报
回复
都是苦逼的孩子, 都是为了生活!!
  感谢 程序员姐姐的 讲解, 小弟我感谢了 ,
清竹小雨 2012-05-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

我不是搞C#的,我是一个苦逼的人。什么东西都要去学。苦恼啊。
引用 3 楼 的回复:

这位姐姐 原来是搞c#的啊?
引用 2 楼 的回复:

C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
……
[/Quote]
有谁不是呢,只要是计算机的,硬件、软件、网络三大版块都要了解
W170532934 2012-05-04
  • 打赏
  • 举报
回复
我不是搞C#的,我是一个苦逼的人。什么东西都要去学。苦恼啊。
[Quote=引用 3 楼 的回复:]

这位姐姐 原来是搞c#的啊?
引用 2 楼 的回复:

C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
……
[/Quote]
猪头小哥 2012-05-04
  • 打赏
  • 举报
回复
这位姐姐 原来是搞c#的啊?
[Quote=引用 2 楼 的回复:]

C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(strin……
[/Quote]
W170532934 2012-05-04
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// ReadBmp();
}
const int BFT_BITMAP=0x4d42; /* 'BMP' */
const int BI_RGB= 0;
const int BI_RLE8= 1;
const int BI_RLE4 =2;
const int BI_BITFIELDS = 3;
int ReadBmp(string FileName, ref Byte[] pImg,ref int width,ref int height)
{
ushort bfType,biBitCount;
uint biSize,dwCompression, biClrUsed, bfOffBits;
int biWidth,biHeight;
uint dwEffWidth;
int i;
FileStream filestream = new FileStream(FileName, FileMode.Open,FileAccess.Read);
BinaryReader bReader = new BinaryReader(filestream);
bReader.BaseStream.Position = 0;
bfType = bReader.ReadUInt16();
if (bfType != BFT_BITMAP)
{
Console.WriteLine("Not bmp File!");
return 0;
}
bReader.BaseStream.Position = 10;
bfOffBits =(uint) bReader.ReadInt32();
biSize = (uint)bReader.ReadInt32();
if (biSize!=40)
{
Console.WriteLine("Not common BITMAPINFOHEADER, BITMAPCOREHEADER=12!");
return 0;
}
biWidth = bReader.ReadInt32();
biHeight = bReader.ReadInt32();
bReader.BaseStream.Position += 2;
biBitCount = (ushort)bReader.ReadInt16();
dwCompression = (uint)bReader.ReadInt32();
bReader.BaseStream.Position += 12;
biClrUsed = (uint)bReader.ReadInt32();
if (dwCompression!=BI_RGB)
{
Console.WriteLine("Not supported Compression!");
bReader.Close();
filestream.Close();
return 0;
}
dwEffWidth = ((uint)(((biBitCount * biWidth) + 31) / 32) * 4);
width = biWidth;
height = biHeight;
Console.WriteLine("{1},{2}", biWidth, biHeight);
bReader.BaseStream.Position += bfOffBits;
for (i = 0; i < biHeight;++i )
{
// bReader.Read(pImg+(biHeight-1-i)*biWidth*3,0,dwEffWidth);
//这地方你自己修改下,把数据的位置放在中间的那个参数就可以了。
}
bReader.Close();
filestream.Close();
return 0;
}
}
}

jim-single 2012-05-04
  • 打赏
  • 举报
回复
自己顶下

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧