我正在尝试升级此工具: http: //www.codeproject.com/Articles/31538/Watermarker-Embedding-image-and-text-watermarks但我对具有索引像素格式的图像有疑问。
我已经尝试了我在这里找到的所有解决方案以及其他解决方案,但我无法让它绘制水印。
以下是需要修复的部分代码:
public void DrawImage(Bitmap watermark)
{
if (watermark == null)
throw new ArgumentOutOfRangeException("Watermark");
if (m_opacity < 0 || m_opacity > 1)
throw new ArgumentOutOfRangeException("Opacity");
if (m_scaleRatio <= 0)
throw new ArgumentOutOfRangeException("ScaleRatio");
// Creates a new watermark with margins (if margins are not specified returns the original watermark)
m_watermark = GetWatermarkImage(watermark);
// Rotates and/or flips the watermark
m_watermark.RotateFlip(m_rotateFlip);
// Calculate watermark position
Point waterPos = GetWatermarkPosition();
// Watermark destination rectangle
Rectangle destRect = new Rectangle(waterPos.X, waterPos.Y, m_watermark.Width, m_watermark.Height);
ColorMatrix colorMatrix = new ColorMatrix(
new float[][] {
new float[] { 1, 0f, 0f, 0f, 0f},
new float[] { 0f, 1, 0f, 0f, 0f},
new float[] { 0f, 0f, 1, 0f, 0f},
new float[] { 0f, 0f, 0f, m_opacity, 0f},
new float[] { 0f, 0f, 0f, 0f, 1}
});
ImageAttributes attributes = new ImageAttributes();
// Set the opacity of the watermark
attributes.SetColorMatrix(colorMatrix);
// Set the transparent color
if (m_transparentColor != Color.Empty)
{
attributes.SetColorKey(m_transparentColor, m_transparentColor);
}
// Draw the watermark
using (Graphics gr = Graphics.FromImage(m_image)) <- Тhis is the place where A Graphics object cannot be created from an image that has an indexed pixel format error occurs.
{
gr.DrawImage(m_watermark, destRect, 0, 0, m_watermark.Width, m_watermark.Height, GraphicsUnit.Pixel, attributes);
}
}
我尝试使用正确的像素格式创建临时位图,但未绘制水印。
Bitmap orig = new Bitmap(m_image);
Bitmap clone = new Bitmap(orig.Width, orig.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
using (Graphics gr = Graphics.FromImage(clone)) {
gr.DrawImage(orig, new Rectangle(0, 0, clone.Width, clone.Height));
}
任何帮助,将不胜感激!