using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.IO.Compression; // 圧縮(Deflate)の為 using System.Runtime.InteropServices; // Marshalの為 namespace Deflate_Test { public partial class Form1 : Form { public Form1() { InitializeComponent(); } struct GetData { // データ取得用構造体 public UInt32 intNum; public uint intFlag; public uint intWidth; public uint intHeight; public UInt32[] intData; // = new UInt32[1280 * 1024]; public void Reset() { // 値を入れる時に必ず実行すること intData = new UInt32[1280 * 1024]; } } private List PGetSaveData = new List(); // 保存用データ(ジェネリック) private void button1_Click(object sender, EventArgs e) { for (uint i = 0; i < 1000; i++) { GetData gd = new GetData(); gd.intNum = i; gd.intFlag = 0xff; gd.intWidth = 1280; // 幅と高さはROIで設定しているので、不要かも??? gd.intHeight = 1024; gd.Reset(); // 構造体の配列の初期化 for (uint j = 0; j < 1280 * 1024; j++) { gd.intData[j] = j; } PGetSaveData.Add(gd); // ジェネリックに追加 } } private void button2_Click(object sender, EventArgs e) { IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(GetData))); byte[] packet = new byte[Marshal.SizeOf(typeof(GetData))]; // 入出力用のストリーム MemoryStream ms = new MemoryStream(); DeflateStream CompStream = new DeflateStream(ms, CompressionMode.Compress, true); try{ for (uint i = 0; i < 1000; i++) { // 構造体をバイト配列に変換 Marshal.StructureToPtr(PGetSaveData[(int)i], ptr, false); Marshal.Copy(ptr, packet, 0, Marshal.SizeOf(typeof(GetData))); // ストリームに圧縮するデータを書き込む CompStream.Write(packet, 0 , packet.Length); // 追記? } }finally{ if(IntPtr.Zero != ptr){Marshal.FreeHGlobal(ptr);} } CompStream.Close(); // 圧縮されたデータをバイト配列で取得 //byte[] dest = ms.ToArray(); // ストリームを開放 ms.Close(); ms.Dispose(); // サイズを比較 label1.Text = (PGetSaveData.Count * Marshal.SizeOf(typeof(GetData))).ToString(); //label2.Text = dest.Length.ToString(); } } }