问题定义:
我有4个PNG图像.我想将它们合并为一个PNG图像,就像这样
-------------------| | || png1 | png2 || | |-------------------| | || png3 | png4 || | |-------------------
题:
这样做的最佳和有效方法是什么(结果图像必须是PNG)?
解决方法// Loads the images to tile (no need to specify PngBitmapDecoder,the correct decoder is automatically selected)BitmapFrame frame1 = BitmapDecoder.Create(new Uri(path1),BitmapCreateOptions.None,BitmapCacheOption.OnLoad).Frames.First();BitmapFrame frame2 = BitmapDecoder.Create(new Uri(path2),BitmapCacheOption.OnLoad).Frames.First();BitmapFrame frame3 = BitmapDecoder.Create(new Uri(path3),BitmapCacheOption.OnLoad).Frames.First();BitmapFrame frame4 = BitmapDecoder.Create(new Uri(path4),BitmapCacheOption.OnLoad).Frames.First();// Gets the size of the images (I assume each image has the same size)int imageWIDth = frame1.PixelWIDth;int imageHeight = frame1.PixelHeight;// Draws the images into a DrawingVisual componentDrawingVisual drawingVisual = new DrawingVisual();using (DrawingContext drawingContext = drawingVisual.Renderopen()){ drawingContext.DrawImage(frame1,new Rect(0,imageWIDth,imageHeight)); drawingContext.DrawImage(frame2,new Rect(imageWIDth,imageHeight)); drawingContext.DrawImage(frame3,imageHeight,imageHeight)); drawingContext.DrawImage(frame4,imageHeight));}// Converts the Visual (DrawingVisual) into a BitmapSourcerendertargetBitmap bmp = new rendertargetBitmap(imageWIDth * 2,imageHeight * 2,96,PixelFormats.Pbgra32);bmp.Render(drawingVisual);// Creates a PngBitmapEncoder and adds the BitmapSource to the frames of the encoderPngBitmapEncoder encoder = new PngBitmapEncoder();encoder.Frames.Add(BitmapFrame.Create(bmp));// Saves the image into a file using the encoderusing (Stream stream = file.Create(pathTileImage)) encoder.Save(stream);总结
以上是内存溢出为你收集整理的c# – 将png图像合并到WPF中的单个图像中全部内容,希望文章能够帮你解决c# – 将png图像合并到WPF中的单个图像中所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)