protected static void DrawImageHelper(ViewContext context,
Image image,
Color remapTransparent,
Rectangle imageRect,
VisualOrientation orientation,
PaletteImageEffect effect,
Color remapColor,
Color remapNew)
{
Debug.Assert(context != null);
// Prevent problems with multiple threads using the same palette images
// by only allowing a single thread to draw the provided image at a time
lock (_threadLock)
{
// Validate reference parameter
if (context == null) throw new ArgumentNullException("context");
// Use image attributes class to modify image drawing for effects
ImageAttributes attribs = new ImageAttributes();
switch (effect)
{
case PaletteImageEffect.Disabled:
attribs.SetColorMatrix(CommonHelper.MatrixDisabled);
break;
case PaletteImageEffect.GrayScale:
attribs.SetColorMatrix(_matrixGrayScale, ColorMatrixFlag.SkipGrays);
break;
case PaletteImageEffect.GrayScaleRed:
attribs.SetColorMatrix(_matrixGrayScaleRed, ColorMatrixFlag.SkipGrays);
break;
case PaletteImageEffect.GrayScaleGreen:
attribs.SetColorMatrix(_matrixGrayScaleGreen, ColorMatrixFlag.SkipGrays);
break;
case PaletteImageEffect.GrayScaleBlue:
attribs.SetColorMatrix(_matrixGrayScaleBlue, ColorMatrixFlag.SkipGrays);
break;
case PaletteImageEffect.Light:
attribs.SetColorMatrix(_matrixLight);
break;
case PaletteImageEffect.LightLight:
attribs.SetColorMatrix(_matrixLightLight);
break;
case PaletteImageEffect.Dark:
attribs.SetColorMatrix(_matrixDark);
break;
case PaletteImageEffect.DarkDark:
attribs.SetColorMatrix(_matrixDarkDark);
break;
case PaletteImageEffect.Inherit:
// Should never happen!
Debug.Assert(false);
break;
}
// Do we need to remap a colors in the bitmap?
if ((remapTransparent != Color.Empty) ||
((remapColor != Color.Empty) && (remapNew != Color.Empty)))
{
List<ColorMap> colorMaps = new List<ColorMap>();
// Create remapping for the transparent color
if (remapTransparent != Color.Empty)
{
ColorMap remap = new ColorMap();
remap.OldColor = remapTransparent;
remap.NewColor = Color.Transparent;
colorMaps.Add(remap);
}
// Create remapping from source to target colors
if ((remapColor != Color.Empty) && (remapNew != Color.Empty))
{
ColorMap remap = new ColorMap();
remap.OldColor = remapColor;
remap.NewColor = remapNew;
colorMaps.Add(remap);
}
attribs.SetRemapTable(colorMaps.ToArray(), ColorAdjustType.Bitmap);
}
int translateX = 0;
int translateY = 0;
float rotation = 0f;
// Perform any transformations needed for orientation
switch (orientation)
{
case VisualOrientation.Bottom:
// Translate to opposite side of origin, so the rotate can
// then bring it back to original position but mirror image
translateX = imageRect.X * 2 + imageRect.Width;
translateY = imageRect.Y * 2 + imageRect.Height;
rotation = 180f;
break;
case VisualOrientation.Left:
// Invert the dimensions of the rectangle for drawing upwards
imageRect = new Rectangle(imageRect.X, imageRect.Y, imageRect.Height, imageRect.Width);
// Translate back from a quarter left turn to the original place
translateX = imageRect.X - imageRect.Y;
translateY = imageRect.X + imageRect.Y + imageRect.Width;
rotation = -90f;
break;
case VisualOrientation.Right:
// Invert the dimensions of the rectangle for drawing upwards
imageRect = new Rectangle(imageRect.X, imageRect.Y, imageRect.Height, imageRect.Width);
// Translate back from a quarter right turn to the original place
translateX = imageRect.X + imageRect.Y + imageRect.Height;
translateY = -(imageRect.X - imageRect.Y);
rotation = 90f;
break;
}
// Apply the transforms if we have any to apply
if ((translateX != 0) || (translateY != 0))
context.Graphics.TranslateTransform(translateX, translateY);
if (rotation != 0f)
context.Graphics.RotateTransform(rotation);
try
{
// Finally, just draw the image and let the transforms do the rest
context.Graphics.DrawImage(image, imageRect, 0, 0, imageRect.Width, imageRect.Height, GraphicsUnit.Pixel, attribs);
}
catch (ArgumentException)
{
}
finally
{
if (rotation != 0f)
context.Graphics.RotateTransform(-rotation);
// Remove the applied transforms
if ((translateX != 0) | (translateY != 0))
context.Graphics.TranslateTransform(-translateX, -translateY);
}
}
}