 Rank: Advanced Member Groups: Administration
, Member
Joined: 7/28/2003 Posts: 1,241 Points: -190 Location: WA, US
|
Aurigma.GraphicsMill.Drawing.GdiGraphics does not support CMYK pixel formats, so if you need to draw a text on a CMYK bitmap, you will need to perform some additional actions.If you attempt to draw text on a bitmap with some of the CMYK pixel formats using the Aurigma.GraphicsMill.Drawing.GdiGraphics class, the following exception will occur: Code:Format not supported The reason to that behavior is that the Aurigma.GraphicsMill.Drawing.GdiGraphics does not support CMYK pixel formats. And when you try to get the GdiGraphics instance to draw a string upon, the error mentioned above occurs. The avoid this situation, you may do the following: - Draw a text string on a temporary transparent RGB bitmap.
- Convert the temporary bitmap to one of the CMYK formats.
- Merge the temporary bitmap with the destination bitmap.
This approach is demonstrated below: Code:
string str = "Aurigma";
Aurigma.GraphicsMill.Drawing.Font font = new Aurigma.GraphicsMill.Drawing.Font("Arial", 14);
font.Antialiased = true;
System.Drawing.SizeF size = font.MeasureText(str);
System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
Aurigma.GraphicsMill.Bitmap bitmap = new Aurigma.GraphicsMill.Bitmap(@"d:/cmyk32.jpg");
Aurigma.GraphicsMill.Bitmap tempBitmap = new
Aurigma.GraphicsMill.Bitmap(System.Convert.ToInt32(size.Width),
System.Convert.ToInt32(size.Height), Aurigma.GraphicsMill.PixelFormat.Format32bppArgb);
System.Drawing.Graphics graphics =
System.Drawing.Graphics.FromImage(tempBitmap.ToGdiplusBitmapDirectly());
try
{
graphics.DrawString(str, font, brush, 0, 0);
}
finally
{
graphics.Dispose();
}
tempBitmap.ColorManagement.ConvertToContinuous(Aurigma.GraphicsMill.ColorSpace.Cmyk,
tempBitmap.HasAlpha, tempBitmap.IsExtended);
tempBitmap.Draw(bitmap, 20, 20, tempBitmap.Width, tempBitmap.Height,
Aurigma.GraphicsMill.Transforms.CombineMode.Alpha, 1.0f,
Aurigma.GraphicsMill.Transforms.InterpolationMode.LowQuality);
bitmap.Save(@"d:/cmyk32-txt.jpg");
Note that in this sample the string is drawn using an RGB color. But what if you want to draw a string using a CMYK color value? In this case you will need to either perform a color conversion (from CMYK to RGB) before drawing the text, or, if you need to keep the exact CMYK value, use a completely different approach to the problem. The idea of this approach is to create an empty ACMYK bitmap filled with the necessary color, and then "cut out" the text in the alpha channel. After that the bitmaps will be merged. However, this approach has its own shortcomings: - You may find the text output quality worse than when using the standard approach (though that is not necessarily so).
- If the text string is large enough, you will have noticeable memory overhead.
Here is a code sample demonstrating this approach: Code:
Aurigma.GraphicsMill.Bitmap bmp = new Aurigma.GraphicsMill.Bitmap(@"d:\cmyk32.jpg");
string str = "Aurigma2";
Aurigma.GraphicsMill.Drawing.Font font = new Aurigma.GraphicsMill.Drawing.Font("Arial", 14);
font.Antialiased = true;
System.Drawing.SizeF size = font.MeasureText(str);
Aurigma.GraphicsMill.CmykColor textColor =
Aurigma.GraphicsMill.CmykColor.FromCmyk(128, 12, 55, 25);
Aurigma.GraphicsMill.Bitmap tempBitmap = new Aurigma.GraphicsMill.Bitmap(textColor,
(int)size.Width, (int)size.Height, Aurigma.GraphicsMill.PixelFormat.Format40bppAcmyk);
Aurigma.GraphicsMill.Bitmap mask = new Aurigma.GraphicsMill.Bitmap((int)size.Width,
(int)size.Height, Aurigma.GraphicsMill.PixelFormat.Format24bppRgb);
Aurigma.GraphicsMill.Drawing.GdiGraphics graphics = mask.GetGdiGraphics();
graphics.DrawString(str, font,
new Aurigma.GraphicsMill.Drawing.SolidBrush(Aurigma.GraphicsMill.RgbColor.White),
new System.Drawing.Point(0, 0));
mask.ColorManagement.ConvertToContinuous(Aurigma.GraphicsMill.ColorSpace.GrayScale,
false, false);
tempBitmap.Channels[Aurigma.GraphicsMill.ColorChannel.Alpha] = mask;
tempBitmap.Draw(bmp, 20, 40, -1, -1, Aurigma.GraphicsMill.Transforms.CombineMode.Alpha,
1, Aurigma.GraphicsMill.Transforms.InterpolationMode.HighQuality);
bmp.Save(@"d:\cmyk32-txt.jpg");
Best regards, Fedor Skvortsov
|
Rank: Advanced Member Groups: Administration
, Member
Joined: 5/15/2007 Posts: 5 Points: 9
|
We glad to introduce you one more approach to draw text on a CMYK bitmap. This approach was suggested by our customer Brad Erickson. His original post is located here: The main idea of this approach can be described as follows: 1. Get each color channel from the source CMYK image. 2. Convert each channel to RGB pixel format. That allows us to draw any graphics on the channels using methods of the GdiGraphics class. 3. Draw the text string on each channel with the specified text colors. In other words, if you need to draw a text with (214, 99, 255, 94) CMYK color you should draw this text with (214, 214, 214) color on the Cyan channel, (99, 99, 99) color on the Magenta channel, (255, 255, 255) color on the Yellow channel, and (94, 94, 94) color on the Black channel. 4. Convert each channel back to GrayScale. 5. Put it back into the original CMYK image. Here is the code sample demonstrating this approach: VB: Code:
Dim cmykImageFilname As String = "..\..\Cmyk32.jpg"
Dim resultImageFilename As String = "..\..\result.jpg"
Dim cmykBitmap As New Aurigma.GraphicsMill.Bitmap(cmykImageFilname)
Dim str As String = "Aurigma3"
Dim font As New System.Drawing.Font("Arial", 42)
Dim textColor As Aurigma.GraphicsMill.CmykColor = _
Aurigma.GraphicsMill.CmykColor.FromCmyk(214, 99, 255, 94)
Dim channels As Aurigma.GraphicsMill.ColorChannel() = {Aurigma.GraphicsMill.ColorChannel.Cyan, _
Aurigma.GraphicsMill.ColorChannel.Magenta, _
Aurigma.GraphicsMill.ColorChannel.Yellow, _
Aurigma.GraphicsMill.ColorChannel.Black}
Dim colors As Integer() = {textColor.C, textColor.M, textColor.Y, textColor.K}
Dim colorChannel As Aurigma.GraphicsMill.Bitmap
Dim i As Integer
For i = 0 To 3
colorChannel = cmykBitmap.Channels(channels(i))
colorChannel.ColorManagement.ConvertToContinuous(Aurigma.GraphicsMill.ColorSpace.Rgb, False, False)
Dim graphics As System.Drawing.Graphics = colorChannel.GetGdiplusGraphics()
Dim brush As New System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(255, colors(i), _
colors(i), colors(i)))
graphics.DrawString(str, font, brush, 30, 30)
colorChannel.ColorManagement.ConvertToContinuous(Aurigma.GraphicsMill.ColorSpace.GrayScale, False, False)
cmykBitmap.Channels(channels(i)) = colorChannel
Next
cmykBitmap.Save(resultImageFilename)
cmykBitmap.Dispose()
C#: Code:
string cmykImageFilname = @"..\..\Cmyk32.jpg";
string resultImageFilename = @"..\..\result.jpg";
Aurigma.GraphicsMill.Bitmap cmykBitmap = new Aurigma.GraphicsMill.Bitmap(cmykImageFilname);
string str = "Aurigma3";
System.Drawing.Font font = new System.Drawing.Font("Arial", 42);
Aurigma.GraphicsMill.CmykColor textColor =
Aurigma.GraphicsMill.CmykColor.FromCmyk(214, 99, 255, 94);
Aurigma.GraphicsMill.ColorChannel[] channels = {Aurigma.GraphicsMill.ColorChannel.Cyan,
Aurigma.GraphicsMill.ColorChannel.Magenta,
Aurigma.GraphicsMill.ColorChannel.Yellow,
Aurigma.GraphicsMill.ColorChannel.Black};
int[] colors = { textColor.C, textColor.M, textColor.Y, textColor.K };
Aurigma.GraphicsMill.Bitmap colorChannel;
for (int i = 0; i < 4; i++)
{
colorChannel = cmykBitmap.Channels[channels[i]];
colorChannel.ColorManagement.ConvertToContinuous(Aurigma.GraphicsMill.ColorSpace.Rgb, false, false);
System.Drawing.Graphics graphics = colorChannel.GetGdiplusGraphics();
System.Drawing.SolidBrush brush = new
System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(255, colors[i], colors[i], colors[i]));
graphics.DrawString(str, font, brush, 30, 30);
colorChannel.ColorManagement.ConvertToContinuous(Aurigma.GraphicsMill.ColorSpace.GrayScale, false, false);
cmykBitmap.Channels[channels[i]] = colorChannel;
}
cmykBitmap.Save(resultImageFilename);
Best regards, Tatyana Bertyakova
|