예제 #1
0
        private PsdFile ConvertToPsd(PsbPainter painter, int width, int height)
        {
            PsdFile psd = new PsdFile
            {
                Width      = width,
                Height     = height,
                Resolution = new ResolutionInfo
                {
                    HeightDisplayUnit = ResolutionInfo.Unit.Centimeters,
                    WidthDisplayUnit  = ResolutionInfo.Unit.Centimeters,
                    HResDisplayUnit   = ResolutionInfo.ResUnit.PxPerInch,
                    VResDisplayUnit   = ResolutionInfo.ResUnit.PxPerInch,
                    HDpi = new UFixed16_16(0, 350),
                    VDpi = new UFixed16_16(0, 350)
                },
                ImageCompression = ImageCompression.Rle
            };

            psd.ImageResources.Add(new XmpResource("")
            {
                XmpMetaString = Resources.Xmp
            });
            psd.BaseLayer.SetBitmap(new Bitmap(width, height, PixelFormat.Format32bppArgb),
                                    ImageReplaceOption.KeepCenter, psd.ImageCompression);

            string currentGroup = "";
            Layer  beginSection = null;

            foreach (var resMd in painter.Resources)
            {
                if (resMd.Label.StartsWith(painter.GroupMark))
                {
                    resMd.Label = resMd.Label.Substring(1);
                }

                string name = $"{resMd.Label}-{resMd.Name}";

                var layer = psd.MakeImageLayer(resMd.ToImage(), name, (int)(resMd.OriginX + width / 2f - resMd.Width / 2f),
                                               (int)(resMd.OriginY + height / 2f - resMd.Height / 2f));
                layer.Visible = resMd.Visible;
                if (resMd.Opacity <= 0)
                {
                    layer.Opacity = 0;
                }
                else
                {
                    layer.Opacity = (byte)(resMd.Opacity / 10.0f * 255);
                }

                if (resMd.MotionName != currentGroup)
                {
                    currentGroup = resMd.MotionName;
                    if (beginSection != null)
                    {
                        psd.Layers.Add(beginSection);
                        beginSection = null;
                    }

                    if (!string.IsNullOrEmpty(currentGroup))
                    {
                        beginSection = psd.MakeSectionLayers(currentGroup, out var endLayer, false);
                        psd.Layers.Add(endLayer);
                    }
                }

                psd.Layers.Add(layer);
            }

            if (beginSection != null)
            {
                psd.Layers.Add(beginSection);
            }

            psd.Layers.Add(psd.MakeImageLayer(
                               GenerateMarkText("Generated by FreeMote, [email protected] ", width, 200), "FreeMote", 0, 0));
            return(psd);
        }