//============================================================ // <T>序列化数据内容到输出流中。</T> // // @param output 输出流 //============================================================ public void Serialize2(IOutput output) { base.Serialize(output); // 写入宽度和高度 int width = _bitmap.Width; int height = _bitmap.Height; output.WriteInt16((short)width); output.WriteInt16((short)height); // 获得编码器 ImageCodecInfo jpgCodec = RBitmap.FindImageCodecInfo(EBitmap.Jpg); // 检测是否有Aplah值 bool channelA = RBitmap.HasChannel(_bitmap, EBitmapChannel.A); if (channelA) { output.WriteInt8(2); } else { output.WriteInt8(1); } // 保存图片RGB信息 using (Bitmap bitmapRgb = new Bitmap(width, height, PixelFormat.Format32bppRgb)){ EncoderParameters parameters = new EncoderParameters(1); parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)_qualityColor); RBitmap.CopyChannels(_bitmap, bitmapRgb, EBitmapChannels.RGB); using (MemoryStream buffer = new MemoryStream()){ bitmapRgb.Save(buffer, jpgCodec, parameters); byte[] data = buffer.ToArray(); output.WriteInt32(data.Length); output.WriteBytes(data, 0, data.Length); } } // 保存图片A信息 if (channelA) { using (Bitmap bitmapA = new Bitmap(width, height, PixelFormat.Format32bppRgb)){ EncoderParameters parameters = new EncoderParameters(1); parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)_qualityAlpha); RBitmap.CopyChannel(_bitmap, EBitmapChannel.A, bitmapA, EBitmapChannels.RGB, _validAlpha); using (MemoryStream buffer = new MemoryStream()){ bitmapA.Save(buffer, jpgCodec, parameters); byte[] data = buffer.ToArray(); output.WriteInt32(data.Length); output.WriteBytes(data, 0, data.Length); } } } }
//============================================================ // <T>加载指定通道的位图文件。</T> // // @param fileName 文件名称 // @param channels 通道 //============================================================ public void LoadFile(string fileName, int channels = ERsBitmapChannel.ChannelsARGB) { using (Bitmap bitmap = new Bitmap(fileName)) { // 获得属性 int width = bitmap.Width; int height = bitmap.Height; if (null == _bitmap) { _bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb); RBitmap.Clear(_bitmap, Color.FromArgb(255, 0, 0, 0).ToArgb()); } // 复制通道 RBitmap.CopyChannels(bitmap, _bitmap, channels); } // 更新属性 Update(); }
//============================================================ // <T>序列化数据内容到输出流中。</T> // // @param output 输出流 //============================================================ public void Serialize2(IOutput output) { // 处理位图 Bitmap bitmap = _bitmap; if ((_validLocation.X != 0) || (_validLocation.Y != 0) || (_validSize.Width != _bitmap.Width) || ((_validSize.Height != _bitmap.Height))) { bitmap = new Bitmap(_validSize.Width, _validSize.Height, PixelFormat.Format32bppArgb); // 复制图片 RBitmap.Copy(_bitmap, new SIntRectangle(_validLocation.X, _validLocation.Y, _validSize.Width, _validSize.Height), bitmap, new SIntPoint2(0, 0)); } // 写入宽度和高度 int width = _bitmap.Width; int height = _bitmap.Height; output.WriteInt16((short)width); output.WriteInt16((short)height); // 写入有效区域 output.WriteInt16((short)_validLocation.X); output.WriteInt16((short)_validLocation.Y); output.WriteInt16((short)_validSize.Width); output.WriteInt16((short)_validSize.Height); // 获得编码器 ImageCodecInfo jpgCodec = RBitmap.FindImageCodecInfo(EBitmap.Jpg); // 检测是否有Aplah值 bool channelA = RBitmap.HasChannel(bitmap, EBitmapChannel.A); if (channelA) { output.WriteInt8(2); } else { output.WriteInt8(1); } // 保存图片RGB信息 using (Bitmap bitmapRgb = new Bitmap(_validSize.Width, _validSize.Height, PixelFormat.Format32bppRgb)) { EncoderParameters parameters = new EncoderParameters(1); parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)_qualityColor); RBitmap.CopyChannels(bitmap, bitmapRgb, EBitmapChannels.RGB); using (MemoryStream buffer = new MemoryStream()) { bitmapRgb.Save(buffer, jpgCodec, parameters); byte[] data = buffer.ToArray(); output.WriteInt32(data.Length); output.WriteBytes(data, 0, data.Length); } } // 保存图片A信息 if (channelA) { using (Bitmap bitmapA = new Bitmap(_validSize.Width, _validSize.Height, PixelFormat.Format32bppRgb)) { EncoderParameters parameters = new EncoderParameters(1); parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)_qualityAlpha); RBitmap.CopyChannel(bitmap, EBitmapChannel.A, bitmapA, EBitmapChannels.RGB, _validAlpha); using (MemoryStream buffer = new MemoryStream()) { bitmapA.Save(buffer, jpgCodec, parameters); byte[] data = buffer.ToArray(); output.WriteInt32(data.Length); output.WriteBytes(data, 0, data.Length); } } } // 释放图片 if (bitmap != _bitmap) { bitmap.Dispose(); bitmap = null; } }