//============================================================ // <T>序列化数据内容到输出流中。</T> // // @param output 输出流 //============================================================ public override void Serialize(IOutput output) { base.Serialize(output); // 写入宽度和高度 int width = _bitmap.Width; int height = _bitmap.Height; output.WriteInt16((short)width); output.WriteInt16((short)height); // 检测是否有Aplah值 bool channelA = RBitmap.HasChannel(_bitmap, EBitmapChannel.A); if (channelA) { output.WriteInt8(2); } else { output.WriteInt8(1); } // 保存图片信息 byte[] data = RBitmap.ToBytes(_bitmap); byte[] dataResult = new byte[data.Length * 2]; int length = RCompressJpg.Compress(dataResult, 0, dataResult.Length, data, 0, data.Length, _bitmap.Width, _bitmap.Height, _qualityColor, 1); output.WriteInt32(length); output.WriteBytes(dataResult, 0, length); // 保存图片A信息 if (channelA) { length = RCompressJpg.Compress(dataResult, 0, dataResult.Length, data, 0, data.Length, _bitmap.Width, _bitmap.Height, _qualityAlpha, 2); output.WriteInt32(length); output.WriteBytes(dataResult, 0, length); } }
//============================================================ // <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 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; } }
//============================================================ // <T>序列化数据内容到输出流中。</T> // // @param output 输出流 //============================================================ public override void Serialize(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); // 检测是否有Aplah值 bool channelA = RBitmap.HasChannel(bitmap, EBitmapChannel.A); if (channelA) { output.WriteInt8(2); } else { output.WriteInt8(1); } // 保存图片信息 byte[] data = RBitmap.ToBytes(bitmap); //if(RResourceManager.IsColoPremultiplied) { // int colorLength = data.Length; // for (int n = 0; n < colorLength; n += 4) { // float a = (float)data[n + 3]; // data[n ] = (byte)(((float)data[n ] * a) / 255.0f); // data[n + 1] = (byte)(((float)data[n + 1] * a) / 255.0f); // data[n + 2] = (byte)(((float)data[n + 2] * a) / 255.0f); // } //}else if(RResourceManager.IsColoSkipProcess) { // int skipAlpha = RResourceManager.ColoSkipAlpha; // int colorLength = data.Length; // for(int n = 0; n < colorLength; n += 4) { // if(data[n + 3] < skipAlpha) { // data[n] = 0; // data[n + 1] = 0; // data[n + 2] = 0; // data[n + 3] = 0; // } // } //} byte[] dataResult = new byte[4096 + data.Length * 2]; int length = RCompressJpg.Compress(dataResult, 0, dataResult.Length, data, 0, data.Length, bitmap.Width, bitmap.Height, _qualityColor, 1); output.WriteInt32(length); output.WriteBytes(dataResult, 0, length); // 保存图片A信息 if (channelA) { length = RCompressJpg.Compress(dataResult, 0, dataResult.Length, data, 0, data.Length, bitmap.Width, bitmap.Height, _qualityAlpha, 3); output.WriteInt32(length); output.WriteBytes(dataResult, 0, length); } // 释放图片 if (bitmap != _bitmap) { bitmap.Dispose(); bitmap = null; } }