private Image Getimage(){ ImageData imageData = new ImageData(imageWIDth,imageHeight,32,palette); int pixelVecLoc=0; for (int h = 0; h<imageHeight && (pixelVecLoc < currentimagePixelVec.size()); h++) { for (int w = 0; w<imageWIDth && (pixelVecLoc < currentimagePixelVec.size()); w++) { int p = 0; Pixel pixel = currentimagePixelVec.get(pixelVecLoc); p = (pixel.Alpha<<24) | (pixel.Red<<16) | (pixel.Green<<8) | pixel.Blue; imageData.setPixel(w,h,p); pixelVecLoc++; } } imageData = imageData.scaledTo(imageScaleWIDth,imageScaleHeight); Image image = ImageDescriptor.createFromImageData(imageData).createImage(); return image; }
然后在Canvas上绘制它:
gc.drawImage(image,0);解决方法 这是一个简短的片段,展示了如何创建自定义RGBA缓冲区并向其写入像素数据( based on this example):
int wIDth = 512;int height = 256;int strIDe = wIDth * 4 + (wIDth % 4);int pixelWIDth = 4; // RGBA (BGRA)byte[] imageData = new byte[wIDth * strIDe]; // raw byte bufferfor (int y = 0; y < height; y++){ int yPos = y * strIDe; for (int x = 0; x < wIDth; x++) { int xPos = yPos + x * pixelWIDth; imageData[xPos + 2] = (byte) (RedValue); // replace *Value with source data imageData[xPos + 1] = (byte) (GreenValue); imageData[xPos ] = (byte) (BlueValue); imageData[xPos + 3] = (byte) (AlphaValue); }}
然后将BitmapSource.Create Method (Int32, Int32, Double, PixelFormat, BitmapPalette, IntPtr, Int32)
方法与PixelFormats一起使用:
BitmapSource bmp = BitmapSource.Create( wIDth,height,96,// Horizontal DPI 96,// Vertical DPI PixelFormats.Bgra32,// 32-bit BGRA null,// no palette imageData,// byte buffer imageData.Length,// buffer size strIDe); // strIDe
请注意,除了代码段中所示的Alpha分量(BGRA)之外,字节顺序是反向的.
要将结果传输到画布,您可以先创建一个Image
,将BitmapSource设置为Source,最后将其添加到画布:
// create image and set image as sourceImage Bmpimg = New Image();Bmpimg.WIDth = wIDth;Bmpimg.Height = height;Bmpimg.source = bmp;// add image to canvascanvas.Children.Add(Bmpimg);Canvas.Setleft(Bmpimg,0); // to set position (x,y)Canvas.Settop(Bmpimg,0);总结
以上是内存溢出为你收集整理的c# – 在Java SWT中使用ImageData在Canvas上显示图像的WPF等价物是什么全部内容,希望文章能够帮你解决c# – 在Java SWT中使用ImageData在Canvas上显示图像的WPF等价物是什么所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)