Exemplo n.º 1
0
        static void BuildAtlasWithGPU(int thumbSize = 96, int dimension = 20)
        {
            using (var memBmp = new WicBitmapSource(thumbSize * dimension, thumbSize * dimension, WicPixelFormat.GUID_WICPixelFormat32bppPRGBA))
            {
                using (var dc = memBmp.CreateDeviceContext())
                {
                    var row  = 0;
                    var col  = 0;
                    var path = @"d:\temp";
                    var sw   = new Stopwatch();
                    sw.Start();
                    foreach (var file in Directory.EnumerateFiles(path, "*.*", SearchOption.TopDirectoryOnly))
                    {
                        var ext = Path.GetExtension(file);
                        if (!WicImagingComponent.DecoderFileExtensions.Contains(ext))
                        {
                            continue;
                        }

                        if (new FileInfo(file).Length < 32)
                        {
                            continue;
                        }

                        using (var bmp = WicBitmapSource.Load(file))
                        {
                            bmp.Scale(thumbSize, WICBitmapInterpolationMode.WICBitmapInterpolationModeFant);
                            if (bmp.PixelFormat != WicPixelFormat.GUID_WICPixelFormat32bppPRGBA)
                            {
                                bmp.ConvertTo(WicPixelFormat.GUID_WICPixelFormat32bppPRGBA);
                            }

                            using (var cb = dc.CreateBitmapFromWicBitmap(bmp.ComObject))
                            {
                                var dr = D2D_RECT_F.Sized(col * thumbSize, row * thumbSize, bmp.Width, bmp.Height);
                                dc.BeginDraw();
                                dc.DrawBitmap(cb, destinationRectangle: dr);
                                dc.EndDraw();
                            }
                        }

                        col++;
                        if (col > dimension)
                        {
                            col = 0;
                            row++;
                        }
                    }

                    Console.WriteLine(sw.Elapsed);
                }
                memBmp.Save("atlas.jpg");
            }
        }