我正在尝试对接收RGBa .png文件的PNG文件进行一些自动处理,并输出两个jpeg文件:1只是RGB通道,另一种只是alpha通道,作为灰度图像
有没有办法在C#中本地执行此操作?如果需要第三方库,只要它是免费/开源的,那就没问题,但我更愿意直接使用GDI或其他东西。
答案 0 :(得分:3)
这是我的工作代码:
/// <summary>
/// Split PNG file into two JPGs (RGB and alpha)
/// </summary>
private void SplitPngFileIntoRGBandAplha(string imagePath)
{
try
{
// Open original bitmap
var bitmap = new Bitmap(imagePath);
// Rectangle
var rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
// Get RGB bitmap
var bitmapInRgbFormat = bitmap.Clone(rect, PixelFormat.Format32bppRgb);
// Read bitmap data
BitmapData bmData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, bitmap.PixelFormat);
// Prepare alpha bitmap
var alphaBitmap = new Bitmap(bmData.Width, bmData.Height, PixelFormat.Format32bppRgb);
for (int y = 0; y <= bmData.Height -1; y++)
{
for (int x = 0; x <= bmData.Width -1; x++)
{
Color PixelColor = Color.FromArgb(Marshal.ReadInt32(bmData.Scan0, (bmData.Stride * y) + (4 * x)));
if (PixelColor.A > 0 & PixelColor.A <= 255)
{
alphaBitmap.SetPixel(x, y, Color.FromArgb(PixelColor.A, PixelColor.A, PixelColor.A, PixelColor.A));
}
else
{
alphaBitmap.SetPixel(x, y, Color.FromArgb(0, 0, 0, 0));
}
}
}
bitmap.UnlockBits(bmData);
// Prepare JPG format
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
var encoder = Encoder.Quality;
var encoderParameters = new EncoderParameters(1);
var encoderParameter = new EncoderParameter(encoder, 100L);
encoderParameters.Param[0] = encoderParameter;
// Save RGB bitmap
bitmapInRgbFormat.Save(imagePath.Replace("png", "jpg"), jgpEncoder, encoderParameters);
// Save Alpha bitmpa
alphaBitmap.Save(imagePath.Replace(".png", "_alpha.jpg"), jgpEncoder, encoderParameters);
bitmap.Dispose();
bitmapInRgbFormat.Dispose();
bitmap.Dispose();
// Delete bitmap
System.IO.File.Delete(imagePath);
}
catch(Exception e)
{
// Handle exception
}
}
答案 1 :(得分:1)
选项 - 加载到位图clone to get RGB only,而不是手动grab bits with LockBits,并提取Alpha通道以从中创建新的灰度位图。
// get RGB copy
var bitmapInRgbFormat = loadedBitmap.Clone(
new Rectangle(0, 0, loadedBitmap.Width, loadedBitmap.Height),
PixelFormat.Format32bppRgb)