このツールをアップグレードしようとしています: 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));
}
どんな助けでも大歓迎です!