久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 C# 中從 byte[] 創建 bmp 文件

How to create a bmp file from byte[] in C#(如何在 C# 中從 byte[] 創建 bmp 文件)
本文介紹了如何在 C# 中從 byte[] 創建 bmp 文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在 TCP 客戶端接收到一個 byte[] 數組.該數組包含一個 24 位 RGB 位圖文件.如何創建具有給定寬度、高度和數據的位圖文件?

I have a byte[] array received in TCP Client.The array contains a 24-bit RGB Bitmap file.How to create that bitmap file with given Width ,Height and data?

在 C++ 中我使用這個

In C++ I use this

int WriteBitmapFile(const char *filename, int width, int height, unsigned char *imageData)
{
FILE             *filePtr;        // file pointer
BITMAPFILEHEADER bitmapFileHeader;    // bitmap file header
BITMAPINFOHEADER bitmapInfoHeader;    // bitmap info header
DWORD                 imageIdx;    // used for swapping RGB->BGR
unsigned char     tempRGB;            // used for swapping

// open file for writing binary mode
filePtr = fopen(filename, "wb");
if (!filePtr)
    return 0;

// define the bitmap file header
bitmapFileHeader.bfSize = sizeof(BITMAPFILEHEADER);
bitmapFileHeader.bfType = 0x4D42;
bitmapFileHeader.bfReserved1 = 0;
bitmapFileHeader.bfReserved2 = 0;
bitmapFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

// define the bitmap information header
bitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapInfoHeader.biPlanes = 1;
bitmapInfoHeader.biBitCount = 32;                        // 24-bit
bitmapInfoHeader.biCompression = BI_RGB;                // no compression
bitmapInfoHeader.biSizeImage = width * abs(height) * 4;    // width * height * (RGB bytes)
bitmapInfoHeader.biXPelsPerMeter = 0;
bitmapInfoHeader.biYPelsPerMeter = 0;
bitmapInfoHeader.biClrUsed = 0;
bitmapInfoHeader.biClrImportant = 0;
bitmapInfoHeader.biWidth = width;                        // bitmap width
bitmapInfoHeader.biHeight = height;                    // bitmap height

// switch the image data from RGB to BGR
for(imageIdx = 0; imageIdx < bitmapInfoHeader.biSizeImage; imageIdx+=4)
{
    tempRGB = imageData[imageIdx];
    imageData[imageIdx] = imageData[imageIdx + 2];
    imageData[imageIdx + 2] = tempRGB;
}

// write the bitmap file header
fwrite(&bitmapFileHeader, 1, sizeof(BITMAPFILEHEADER), filePtr);

// write the bitmap info header
fwrite(&bitmapInfoHeader, 1, sizeof(BITMAPINFOHEADER), filePtr);

// write the image data
fwrite(imageData, 1, bitmapInfoHeader.biSizeImage, filePtr);

// close our file
fclose(filePtr);

// Success
return 1;
}

我怎么能在 C# 中做到這一點?

How could I do that in C#?

推薦答案

如果數組實際上包含一個位圖文件,那么您可以將字節保存為文件:

If the array actually contains a bitmap file, then you can just save the bytes as a file:

File.WriteAllBytes(fileName, imageData);

如果數組只包含原始像素數據,您可以使用該數據創建一個 Bitmap 對象:

If the array contains only raw pixel data, you can create a Bitmap object using the data:

unsafe {
   fixed (byte* ptr = imageData) {
      using (Bitmap image = new Bitmap(width, height, stride, PixelFormat.Format24bppRgb, new IntPtr(ptr))) {
         image.Save(fileName);
      }
   }
}

stride 值是掃描線之間的字節數.如果掃描線之間沒有填充,則為 24bpp 格式的 width * 3.

The stride value is the number of bytes between the scan lines. If there is no padding between the scan lines, it's width * 3 for a 24bpp format.

此方法使用數組中的數據,而無需在內存中創建整個圖像的另一個副本(這就是它需要步幅值的原因).

This method uses the data in the array without creating another copy of the entire image in memory (which is why it needs the stride value).

如果位圖數據倒置存放在數組中,stride的值應該是負的,指針應該是內存中最后一個掃描行的開始(ptr + stride* (高度 - 1)).

If the bitmap data is stored upside down in the array, the stride value should be negative, and the pointer should be the start of the last scan line in memory (ptr + stride * (height - 1)).

這篇關于如何在 C# 中從 byte[] 創建 bmp 文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Right-click on a Listbox in a Silverlight 4 app(右鍵單擊 Silverlight 4 應用程序中的列表框)
WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在頂部菜單上滾動)
C# Console app - How do I make an interactive menu?(C# 控制臺應用程序 - 如何制作交互式菜單?)
How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中創建重復的表單?)
UI Automation Control Desktop Application and Click on Menu Strip(UI自動化控制桌面應用程序并單擊菜單條)
Removing thin border around the menuitems(刪除菜單項周圍的細邊框)
主站蜘蛛池模板: 色综合一区二区三区 | 久久久久久久久久久久一区二区 | 久久精品亚洲一区 | 国产精彩视频 | 久久精品国产免费 | 亚洲一区二区在线 | 91精品国产综合久久精品 | 日本成年免费网站 | 欧美日韩高清一区 | 在线a视频网站 | 日日日日操| 中国一级特黄毛片大片 | 日韩精品久久 | 日韩一区在线播放 | 久久国| 国产成人精品区一区二区不卡 | 国产一区二区日韩 | 久草免费在线视频 | 午夜天堂精品久久久久 | 久久国产婷婷国产香蕉 | 久久久久成人精品免费播放动漫 | 日韩在线h| 99re6在线视频 | 中文字幕av色 | av网站免费在线观看 | 国产探花在线观看视频 | 精品视频成人 | 久久精品免费一区二区三 | 亚洲国产aⅴ成人精品无吗 欧美激情欧美激情在线五月 | 国产成人免费视频网站高清观看视频 | 日韩在线播放第一页 | 国产激情视频在线观看 | 99久久精品国产一区二区三区 | 久久国产精品99久久久久 | 色偷偷人人澡人人爽人人模 | 成人精品视频99在线观看免费 | 欧美a在线| 午夜精品久久久久久久久久久久久 | 在线视频91 | 成人精品视频在线 | 亚洲精品久久久久中文字幕欢迎你 |