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


C# System.Drawing.Bitmap.ByteSize方法代码示例

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


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

示例1: FromSysBMP

        /// <summary>
        /// Converts a <see cref="System.Drawing.Bitmap"/> object into a <see cref="SharpDX.Direct2D1.Bitmap"/> object for rendering.
        /// </summary>
        /// <param name="renderTarget">The <see cref="SharpDX.Direct2D1.RenderTarget"/> object that images are drawn on.</param>
        /// <param name="bmp">The .NET native bitmap image.</param>
        /// <returns>A <see cref="SharpDX.Direct2D1.Bitmap"/> containing the image data retrieved from the <see cref="System.Drawing.Bitmap"/> object.</returns>
        public static Bitmap FromSysBMP(RenderTarget renderTarget, SystemBitmap bmp)
        {
            var imageArea = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);
            var bmpProps = new BitmapProperties(new SharpDX.Direct2D1.PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied));

            // Unmanaged buffer needs proper disposal
            using (var dataStream = new DataStream(bmp.ByteSize(), true, true))
            {
                var bmpData = bmp.LockBits(imageArea, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
                for (int y = 0; y < bmp.Height; y++)
                {
                    int offset = y * bmpData.Stride;
                    for (int x = 0; x < bmp.Width; x++)
                    {
                        // System bitmaps store pixel values as BGRA byte arrays
                        byte b = Marshal.ReadByte(bmpData.Scan0, offset++);
                        byte g = Marshal.ReadByte(bmpData.Scan0, offset++);
                        byte r = Marshal.ReadByte(bmpData.Scan0, offset++);
                        byte a = Marshal.ReadByte(bmpData.Scan0, offset++);

                        // Convert BGRA to RGBA (stored as a 32 bit = 4 byte integer)
                        int rgba = r | (g << 8) | (b << 16) | (a << 24);

                        // Write the RGBA value to the data buffer
                        dataStream.Write(rgba);
                    }
                }

                // Release the lock on the bitmap & reset the buffer position pointer
                bmp.UnlockBits(bmpData);
                dataStream.Position = 0;

                return new Bitmap(renderTarget, new Size2(bmp.Width, bmp.Height), dataStream, bmpData.Stride, bmpProps);
            }
        }
开发者ID:JoshGrooms,项目名称:Tesseract,代码行数:41,代码来源:BitmapTools.cs


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