当前位置: 首页>>代码示例>>C#>>正文


C# WriteableBitmap.SetPixel方法代码示例

本文整理汇总了C#中Windows.UI.Xaml.Media.Imaging.WriteableBitmap.SetPixel方法的典型用法代码示例。如果您正苦于以下问题:C# WriteableBitmap.SetPixel方法的具体用法?C# WriteableBitmap.SetPixel怎么用?C# WriteableBitmap.SetPixel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Windows.UI.Xaml.Media.Imaging.WriteableBitmap的用法示例。


在下文中一共展示了WriteableBitmap.SetPixel方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DetectWBByteOrder

        /// <summary>
        /// Detects the color order of a stored byte array.  Byte order may change between platforms, you should call this once before writting a PNG or if you have any issues with colors changing.
        /// </summary>
        public static void DetectWBByteOrder()
        {
            // We should only ever run the detection once (assuming it succeeded at least).
            if (WBBODetectionRun == true)
            {
                return;
            }

            // Create a 3x1 WriteableBitmap to write RGB colors to.
            var TestWB = new WriteableBitmap(3, 1);

            //// Create the red 1 pixel rectangle.
            //var redRectangle = new Rectangle();
            //redRectangle.Width = 1;
            //redRectangle.Height = 1;
            //redRectangle.Fill = new SolidColorBrush( Colors.Red );

            //// Create the green 1 pixel rectangle.
            //var greenRectangle = new Rectangle();
            //greenRectangle.Width = 1;
            //greenRectangle.Height = 1;
            //greenRectangle.Fill = new SolidColorBrush( Colors.Green );

            //// Create the blue 1 pixel rectangle.
            //var blueRectangle = new Rectangle();
            //blueRectangle.Width = 1;
            //blueRectangle.Height = 1;
            //blueRectangle.Fill = new SolidColorBrush( Colors.Blue );

            // Render the three 1 px rectangles.
            TestWB.SetPixel(0, 0, Colors.Red);
            TestWB.SetPixel(1, 0, Colors.Green);
            TestWB.SetPixel(2, 0, Colors.Blue);

            // Invalidate the bitmap to make it actually render.
            TestWB.Invalidate();

            // Go get the 4 byte arrays of each red/green/blue pixels that we just rendered.
            var redColor = TestWB.GetPixel(0, 0);
            var greenColor = TestWB.GetPixel(1, 0);
            var blueColor = TestWB.GetPixel(2, 0);

            byte[] redBytes = { redColor.A, redColor.R, redColor.G, redColor.B };
            byte[] greenBytes = { greenColor.A, greenColor.R, greenColor.G, greenColor.B };
            byte[] blueBytes = { blueColor.A, blueColor.R, blueColor.G, blueColor.B };


            // Just in case something goes terrible wrong, set all the color values to invalid settings.
            int trans = 4;
            int red = 4;
            int green = 4;
            int blue = 4;

            // Find the alpha channel, this wil be the only byte that is the same in all three pixels.
            if (redBytes[0] == greenBytes[0] && blueBytes[0] == greenBytes[0]) { trans = 0; }
            if (redBytes[1] == greenBytes[1] && blueBytes[1] == greenBytes[1]) { trans = 1; }
            if (redBytes[2] == greenBytes[2] && blueBytes[2] == greenBytes[2]) { trans = 2; }
            if (redBytes[3] == greenBytes[3] && blueBytes[3] == greenBytes[3]) { trans = 3; }

            // if we didn't detect the alpha channel, just give up :(
            if (trans != 4)
            {
                // now set all the alpha channel's to zero to get them out of the way.
                redBytes[trans] = 0;
                greenBytes[trans] = 0;
                blueBytes[trans] = 0;

                // Find the red channel.
                if (redBytes[0] == 255) { red = 0; }
                else if (redBytes[1] == 255) { red = 1; }
                else if (redBytes[2] == 255) { red = 2; }
                else if (redBytes[3] == 255) { red = 3; }


                // Find the green channel, note that Colors.Green is not dark green, but light green so use 128 instead of 255 to detect it.
                if (greenBytes[0] == 128) { green = 0; }
                else if (greenBytes[1] == 128) { green = 1; }
                else if (greenBytes[2] == 128) { green = 2; }
                else if (greenBytes[3] == 128) { green = 3; }


                // Find the blue channel.
                if (blueBytes[0] == 255) { blue = 0; }
                else if (blueBytes[1] == 255) { blue = 1; }
                else if (blueBytes[2] == 255) { blue = 2; }
                else if (blueBytes[3] == 255) { blue = 3; }
            }

            // Now set the byte order, if any of the values are still set to 4, something went wrong and return the default values.
            if (red == 4 || green == 4 || blue == 4 || trans == 4)
            {
                WBByteOrder = new int[] { 2, 1, 0, 3 };
            }
            else
            {
                WBBODetectionRun = true;
                WBByteOrder = new int[] { red, green, blue, trans };
//.........这里部分代码省略.........
开发者ID:Catrobat,项目名称:CatrobatForWindows,代码行数:101,代码来源:ToolStackPNGWriterLib.cs


注:本文中的Windows.UI.Xaml.Media.Imaging.WriteableBitmap.SetPixel方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。