示例#1
0
        /// <summary>
        /// Combines the images to one big image.
        /// </summary>
        /// <param name="infos">Tuple containing the image
        /// and text to stitch together.</param>
        /// <returns>Task.</returns>
        private async Task <PngBitmapEncoder> StitchImagesTogether(List <Tuple <Uri, string> > infos)
        {
            BitmapFrame[] frames = new BitmapFrame[infos.Count];
            for (int i = 0; i < frames.Length; i++)
            {
                frames[i] = BitmapDecoder.Create(infos[i].Item1 ?? (SelectedCollageType == CollageType.Albums ? new Uri("pack://application:,,,/Resources/noalbumimage.png") : new Uri("pack://application:,,,/Resources/noartistimage.png")), BitmapCreateOptions.None, BitmapCacheOption.OnDemand).Frames.First();
            }

            OnStatusUpdated("Downloading images...");
            while (frames.Any(f => f.IsDownloading))
            {
                await Task.Delay(100);
            }

            int imageWidth  = frames[0].PixelWidth;
            int imageHeight = frames[0].PixelHeight;

            int           col = (int)SelectedCollageSize;
            DrawingVisual dv  = new DrawingVisual();

            using (DrawingContext dc = dv.RenderOpen())
            {
                int cnt = 0;
                for (int y = 0; y < col; y++)
                {
                    for (int x = 0; x < col; x++)
                    {
                        dc.DrawImage(frames[cnt], new Rect(x * imageWidth, y * imageHeight, imageWidth, imageHeight));

                        if (!string.IsNullOrEmpty(infos[cnt].Item2))
                        {
                            // create text
                            FormattedText extraText = new FormattedText(infos[cnt].Item2, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Verdana"), 14, Brushes.Black)
                            {
                                MaxTextWidth  = imageWidth,
                                MaxTextHeight = imageHeight
                            };

                            dc.DrawText(extraText, new Point(x * imageWidth + 1, y * imageHeight + 1));
                            extraText.SetForegroundBrush(Brushes.White);
                            dc.DrawText(extraText, new Point(x * imageWidth, y * imageHeight));
                        }

                        cnt++;
                    }
                }
            }

            // Converts the Visual (DrawingVisual) into a BitmapSource
            RenderTargetBitmap bmp = new RenderTargetBitmap(imageWidth * col, imageHeight * col, 96, 96, PixelFormats.Pbgra32);

            bmp.Render(dv);

            // Creates a PngBitmapEncoder and adds the BitmapSource to the frames of the encoder
            PngBitmapEncoder encoder = new PngBitmapEncoder();

            encoder.Frames.Add(BitmapFrame.Create(bmp));

            return(encoder);
        }