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


C# FileStream.WriteShort方法代码示例

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


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

示例1: Main

        static int Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("You need to include at least one GIF, JPEG, PNG, TIFF, WMF, EMF, BMP, or ICO file on the command line");
            }
            for (int i = 0; i < args.Length; i++)
            {
                Bitmap img = null;
                FileStream bmp = null;
                try
                {
                    // Open the source image
                    img = new Bitmap(args[i]);

                    // Get the name of the new file (replace extension with .bmp)
                    String name = args[i];
                    int dot = name.LastIndexOf(".");
                    if (dot > 0)
                        name = name.Remove(dot);
                    name += ".bmp";

                    // Calculate the size of the bitmap data in the image
                    int w = img.Width, h = img.Height, bmpSize = w * h * 4;

                    // Open the BMP file
                    bmp = new FileStream(name, FileMode.Create, FileAccess.Write);

                    // Write BMP Magic Number
                    bmp.WriteByte((byte)'B');
                    bmp.WriteByte((byte)'M');

                    // Write BMP File Header
                    bmp.WriteLong(54+bmpSize);  // Size of the entire file
                    bmp.WriteLong(0);           // Reserved WORDs 1 and 2
                    bmp.WriteLong(54);          // Bitmap data start (always 54)

                    // Write Bitmap data header
                    bmp.WriteLong(40);      // Header size (40 for Windows V3 / BITMAPINFOHEADER)
                    bmp.WriteLong(w);       // Image width
                    bmp.WriteLong(h);       // Image height
                    bmp.WriteShort(1);      // Number of color planes (must be 1)
                    bmp.WriteShort(32);     // Bits per pixel (32 for full color and alpha images)
                    bmp.WriteLong(0);       // Compression type (0 for no compression)
                    bmp.WriteLong(bmpSize); // Size of the bitmap data
                    bmp.WriteLong(2835);    // Horizontal resolution of image (2835 px/m is typical)
                    bmp.WriteLong(2835);    // Vertical resolution of image (2835 px/m is typical)
                    bmp.WriteLong(0);       // Number of colors in palette (0 for no palette)
                    bmp.WriteLong(0);       // Number of important colors in palette (0 for all colors are important)

                    // Write bitmap data
                    bmp.Seek(54, SeekOrigin.Begin);
                    for (int y = h - 1; y >= 0; y--)
                    {
                        for (int x = 0; x < w; x++)
                        {
                            Color c = img.GetPixel(x, y);
                            byte[] px = { c.B, c.G, c.R, c.A }; // BMP file is backwards (BGRA instead of ARGB)
                            px[0] = (byte)(px[0] * px[3] / 255); // We must pre-multiply the alpha values
                            px[1] = (byte)(px[1] * px[3] / 255);
                            px[2] = (byte)(px[2] * px[3] / 255);
                            bmp.Write(px, 0, 4); // Write the pixel
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("There was a problem converting " + args[i] + ": " + ex.Message);
                }
                finally
                {
                    if (bmp != null) bmp.Dispose();
                    if (img != null) img.Dispose();
                }
            }

            return 0;
        }
开发者ID:coderforlife,项目名称:c4l-utils,代码行数:78,代码来源:Program.cs


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