Nhiệm vụ của tôi là giải nén gói (đã nhận) bằng zlib và sau đó sử dụng thuật toán để tạo ảnh từ dữ liệuzlib từ C++ đến C# (Cách chuyển đổi byte [] thành luồng và luồng thành byte [])
Tin tốt lành là tôi có mã trong C++, nhưng nhiệm vụ là để làm điều đó trong C#
C++
//Read the first values of the packet received
DWORD image[200 * 64] = {0}; //used for algoritm(width always = 200 and height always == 64)
int imgIndex = 0; //used for algoritm
unsigned char rawbytes_[131072] = {0}; //read below
unsigned char * rawbytes = rawbytes_; //destrination parameter for decompression(ptr)
compressed = r.Read<WORD>(); //the length of the compressed bytes(picture)
uncompressed = r.Read<WORD>(); //the length that should be after decompression
width = r.Read<WORD>(); //the width of the picture
height = r.Read<WORD>(); //the height of the picture
LPBYTE ptr = r.GetCurrentStream(); //the bytes(file that must be decompressed)
outLen = uncompressed; //copy the len into another variable
//Decompress
if(uncompress((Bytef*)rawbytes, &outLen, ptr, compressed) != Z_OK)
{
printf("Could not uncompress the image code.\n");
Disconnect();
return;
}
//Algoritm to make up the picture
// Loop through the data
for(int c = 0; c < (int)height; ++c)
{
for(int r = 0; r < (int)width; ++r)
{
imgIndex = (height - 1 - c) * width + r;
image[imgIndex] = 0xFF000000;
if(-((1 << (0xFF & (r & 0x80000007))) & rawbytes[((c * width + r) >> 3)]))
image[imgIndex] = 0xFFFFFFFF;
}
}
tôi đang cố gắng để làm điều này với zlib.NET, nhưng tất cả demo có mã đó để giải nén (C#)
private void decompressFile(string inFile, string outFile)
{
System.IO.FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.Create);
zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outFileStream);
System.IO.FileStream inFileStream = new System.IO.FileStream(inFile, System.IO.FileMode.Open);
try
{
CopyStream(inFileStream, outZStream);
}
finally
{
outZStream.Close();
outFileStream.Close();
inFileStream.Close();
}
}
public static void CopyStream(System.IO.Stream input, System.IO.Stream output)
{
byte[] buffer = new byte[2000];
int len;
while ((len = input.Read(buffer, 0, 2000)) > 0)
{
output.Write(buffer, 0, len);
}
output.Flush();
}
Vấn đề của tôi: Tôi không muốn lưu tệp sau khi giải nén, vì tôi phải sử dụng thuật toán được hiển thị trong mã C++.
Làm thế nào để chuyển đổi mảng byte [] thành một dòng tương tự như một trong mã zlib C# để giải nén dữ liệu và sau đó làm thế nào để chuyển đổi dòng trở lại thành mảng byte?
Ngoài ra, Cách thay đổi mã zlib.NET để KHÔNG lưu tệp?
Cần chính xác như nhau. Cảm ơn! – lesderid
Tôi muốn tặng một phần thưởng cho @JoshStribling - Tôi có thể làm điều đó vào ngày mai. –