Rank: Advanced Member
Groups: Member
Joined: 5/14/2007(UTC) Posts: 24
Was thanked: 1 time(s) in 1 post(s)
|
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"
' Load source CMYK bitmap.
Dim cmykBitmap As New Aurigma.GraphicsMill.Bitmap(cmykImageFilname)
' Configure the text settings.
Dim str As String = "Aurigma3"
Dim font As New System.Drawing.Font("Arial", 42)
' Set the text color.
Dim textColor As Aurigma.GraphicsMill.CmykColor = _
Aurigma.GraphicsMill.CmykColor.FromCmyk(214, 99, 255, 94)
' Get the color channels.
Dim channels As Aurigma.GraphicsMill.ColorChannel() = {Aurigma.GraphicsMill.ColorChannel.Cyan, _
Aurigma.GraphicsMill.ColorChannel.Magenta, _
Aurigma.GraphicsMill.ColorChannel.Yellow, _
Aurigma.GraphicsMill.ColorChannel.Black}
' Get the colors.
Dim colors As Integer() = {textColor.C, textColor.M, textColor.Y, textColor.K}
' Create bitmap for work with each color channel.
Dim colorChannel As Aurigma.GraphicsMill.Bitmap
' Process each color channel.
Dim i As Integer
For i = 0 To 3
colorChannel = cmykBitmap.Channels(channels(i))
' Convert current color channel to RGB, because of GetGdiplusGraphics can work with
' GrayScale bitmaps.
colorChannel.ColorManagement.ConvertToContinuous(Aurigma.GraphicsMill.ColorSpace.Rgb, False, False)
' Get the GDI+ Graphics for the colorChannel and draw the string on it.
Dim graphics As System.Drawing.Graphics = colorChannel.GetGdiplusGraphics()
' Create a solid brush colored with specified RGB color and complete opacity.
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)
' Convert colorChannel form RGB to GrayScale.
colorChannel.ColorManagement.ConvertToContinuous(Aurigma.GraphicsMill.ColorSpace.GrayScale, False, False)
' Put the channel back to the image.
cmykBitmap.Channels(channels(i)) = colorChannel
Next
cmykBitmap.Save(resultImageFilename)
cmykBitmap.Dispose()
C#: Code:
string cmykImageFilname = @"..\..\Cmyk32.jpg";
string resultImageFilename = @"..\..\result.jpg";
// Load source CMYK bitmap.
Aurigma.GraphicsMill.Bitmap cmykBitmap = new Aurigma.GraphicsMill.Bitmap(cmykImageFilname);
// Configure the text settings.
string str = "Aurigma3";
System.Drawing.Font font = new System.Drawing.Font("Arial", 42);
// Set the text color.
Aurigma.GraphicsMill.CmykColor textColor =
Aurigma.GraphicsMill.CmykColor.FromCmyk(214, 99, 255, 94);
// Get the color channels.
Aurigma.GraphicsMill.ColorChannel[] channels = {Aurigma.GraphicsMill.ColorChannel.Cyan,
Aurigma.GraphicsMill.ColorChannel.Magenta,
Aurigma.GraphicsMill.ColorChannel.Yellow,
Aurigma.GraphicsMill.ColorChannel.Black};
// Get the colors.
int[] colors = { textColor.C, textColor.M, textColor.Y, textColor.K };
// Create bitmap for work with each color channel.
Aurigma.GraphicsMill.Bitmap colorChannel;
// Process each color channel.
for (int i = 0; i < 4; i++)
{
colorChannel = cmykBitmap.Channels[channels[i]];
// Convert current color channel to RGB, because of GetGdiplusGraphics can work with
// GrayScale bitmaps.
colorChannel.ColorManagement.ConvertToContinuous(Aurigma.GraphicsMill.ColorSpace.Rgb, false, false);
// Get the GDI+ Graphics for the colorChannel and draw the string on it.
System.Drawing.Graphics graphics = colorChannel.GetGdiplusGraphics();
// Create a solid brush colored with specified RGB color and complete opacity.
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);
// Convert colorChannel form RGB to GrayScale.
colorChannel.ColorManagement.ConvertToContinuous(Aurigma.GraphicsMill.ColorSpace.GrayScale, false, false);
// Put the channel back to the image.
cmykBitmap.Channels[channels[i]] = colorChannel;
}
cmykBitmap.Save(resultImageFilename);
Edited by user Monday, April 28, 2008 4:34:25 PM(UTC)
| Reason: Not specified |