This forum contains outdated content and is available for reading only. Please contact technical support if you have any questions.

Replacing an image in a layer using Advanced PSD Add-in

Hi, I cannot seem to find any reference to replacing an image on a layer that is in a PSD file. I want users to be able to select an image (that they have previously uploaded) and type a caption into a text box and then merge the image and text using the PSD Add-in to create the resultant jpg. I'd have a template PSD file which would contain a blank layer for the image and a text placeholder.

Is this possible and does anyone have any examples?

thanks in advance

Peter
Hello,

We will post sample in a few days.
Hello,

Here is the code sample.

It uses Template.psd as a template to create new jpg file which contains hardcoded image and text.



        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

Attachments