Rank: Advanced Member
Groups: Guest
Joined: 5/14/2007(UTC) Posts: 24
Was thanked: 1 time(s) in 1 post(s)
|
Hello, Here is the code sample. It uses Template.psd as a template to create new jpg file which contains hardcoded image and text. Code:
private void NewProcessPsd()
{
string newText = "Some text text text";
Aurigma.GraphicsMill.Bitmap newImage = new Aurigma.GraphicsMill.Bitmap(@"C:\temp\IMG_2516.jpg");
Aurigma.GraphicsMill.Codecs.AdvancedPsdReader psdReader = new Aurigma.GraphicsMill.Codecs.AdvancedPsdReader(@"C:\temp\template.psd");
//Determining PSD pixel format
Aurigma.GraphicsMill.Codecs.AdvancedPsdFrame frame0 = (Aurigma.GraphicsMill.Codecs.AdvancedPsdFrame)psdReader.LoadFrame(0);
Aurigma.GraphicsMill.PixelFormat psdPixelFormat = frame0.PixelFormat;
//Creating result bitmap
Aurigma.GraphicsMill.Bitmap resultImage = new Aurigma.GraphicsMill.Bitmap(psdReader.Width, psdReader.Height, psdPixelFormat);
resultImage.HorizontalResolution = frame0.HorizontalResolution;
resultImage.VerticalResolution = frame0.VerticalResolution;
//draw background image
Aurigma.GraphicsMill.Codecs.AdvancedPsdFrame backgroundFrame = (Aurigma.GraphicsMill.Codecs.AdvancedPsdFrame)psdReader.LoadFrame(1);
if (backgroundFrame.Type == Aurigma.GraphicsMill.Codecs.PsdFrameType.Raster)
{
Aurigma.GraphicsMill.Bitmap rasterLayer = new Aurigma.GraphicsMill.Bitmap();
backgroundFrame.GetBitmap(rasterLayer);
DrawLayerOnImage(resultImage, rasterLayer, backgroundFrame.Left, backgroundFrame.Top, 1.0f);
}
else
{
MessageBox.Show("The first layer should be a background");
return;
}
//draw text
Aurigma.GraphicsMill.Codecs.AdvancedPsdTextFrame textFrame = (Aurigma.GraphicsMill.Codecs.AdvancedPsdTextFrame)psdReader.LoadFrame(2);
if (textFrame.Type == Aurigma.GraphicsMill.Codecs.PsdFrameType.Text)
{
Aurigma.GraphicsMill.Drawing.GdiGraphics graphics = resultImage.GetGdiGraphics();
int textY = (int)textFrame.TextBox.Top;
int textX = (int)textFrame.TextBox.Left;
int textWidth = (int)textFrame.TextBox.Width;
int textHeight = (int)textFrame.TextBox.Height;
Aurigma.GraphicsMill.RgbColor rgbColor = (Aurigma.GraphicsMill.RgbColor)textFrame.Color.ConvertColorSpace(Aurigma.GraphicsMill.ColorSpace.Rgb);
Aurigma.GraphicsMill.Drawing.SolidBrush brush = new Aurigma.GraphicsMill.Drawing.SolidBrush(rgbColor);
System.Drawing.RectangleF blackBox = textFrame.Font.GetBlackBox(textFrame.Text);
textY = textY + (int)blackBox.Y;
textHeight = textHeight + (int)textFrame.Font.Size - (int)blackBox.Height;
graphics.DrawString(newText, textFrame.Font, brush, new System.Drawing.RectangleF(textX, textY, textWidth, textHeight), Aurigma.GraphicsMill.Drawing.TextTrimmingMode.EndEllipsis, false, false, false);
}
else
{
MessageBox.Show("The second layer should be a text");
return;
}
//draw image
Aurigma.GraphicsMill.Codecs.AdvancedPsdFrame imageFrame = (Aurigma.GraphicsMill.Codecs.AdvancedPsdFrame)psdReader.LoadFrame(3);
if (imageFrame.Type == Aurigma.GraphicsMill.Codecs.PsdFrameType.Raster)
{
newImage.Transforms.Resize(imageFrame.Width, imageFrame.Height, Aurigma.GraphicsMill.Transforms.InterpolationMode.HighQuality);
DrawLayerOnImage(resultImage, newImage, imageFrame.Left, imageFrame.Top, 1.0f);
}
else
{
MessageBox.Show("The third layer should be an image");
return;
}
//Save result jpg
resultImage.Save(@"C:\temp\test.jpg");
}
private void DrawLayerOnImage(Aurigma.GraphicsMill.Bitmap image, Aurigma.GraphicsMill.Bitmap layer, int layerLeft, int layerTop, float layerOpacity)
{
if (image.IsEmpty || layer.IsEmpty)
throw new ApplicationException("Cannot draw layer. Image and layer bitmap cannot be empty.");
if (image.IsCmyk && layer.IsRgb)
layer.ColorManagement.ConvertToContinuous(Aurigma.GraphicsMill.ColorSpace.Cmyk, true, false);
layer.Draw(image, layerLeft, layerTop, layer.Width, layer.Height, Aurigma.GraphicsMill.Transforms.CombineMode.Alpha, layerOpacity, Aurigma.GraphicsMill.Transforms.InterpolationMode.HighSpeed);
layer.Dispose();
}
Find template.psd in attachement. Best regards, Tatyana Bertyakova Edited by user Tuesday, December 18, 2007 3:09:53 AM(UTC)
| Reason: Not specified |
|