BarTender .btw文件生成缩略图

        public static Bitmap GetBarTenderThumbnailImage(string FileName,int width, int height, Color backgroundColor)
        {
            Color DefaultBackground = Color.FromArgb(112, 112, 112);
            if (width < 0 || height < 0)
            {
                throw new BadImageFormatException("Bad thumbnail size was requested.");
            }


            if (string.IsNullOrEmpty(FileName))
            {
                throw new FileNotFoundException();
            }
            Bitmap DocumentImage ;

            #region Par

            try
            {
                using (FileStream fileStream = File.OpenRead(FileName))
                {
                    using (BinaryReader binaryReader = new BinaryReader(fileStream))
                    {
                        fileStream.Seek(2L, SeekOrigin.Current);
                        char[] value = binaryReader.ReadChars(3);
                        string text = new string(value);
                        if (text != "Bar")
                        {
                            throw new FileFormatException("This is not a BarTender format file");
                        }
                        while (binaryReader.ReadByte() != 26)
                        {
                        }
                        uint Pnum = binaryReader.ReadUInt32();
                        int Pnum2 = 0;
                        if (Pnum >= 75003)
                        {
                            Pnum2 = binaryReader.ReadInt32();
                            int Pnum3 = Pnum2 * 8;
                            fileStream.Seek(Pnum3, SeekOrigin.Current);
                        }

                        #region DeserializeComment
                        int DPnum = 1;
                        byte[] array = new byte[4];
                        binaryReader.Read(array, 0, 3);
                        if (array[0] == byte.MaxValue && array[1] == 254 && array[2] == byte.MaxValue)
                        {
                            DPnum = 2;
                        }
                        else
                        {
                            binaryReader.BaseStream.Seek(-3L, SeekOrigin.Current);
                        }
                        byte b = binaryReader.ReadByte();
                        int DPnum2 = 0;
                        _ = string.Empty;
                        if (b == byte.MaxValue)
                        {
                            short Pnum3 = binaryReader.ReadInt16();
                            if (Pnum3 == Convert.ToUInt16(65535))
                            {
                                int Pnum4 = binaryReader.ReadInt32();
                                DPnum2 = Pnum4;
                            }
                            else
                            {
                                DPnum2 = Pnum3;
                            }
                        }
                        else
                        {
                            DPnum2 = b;
                        }
                        byte[] array2 = new byte[DPnum2 * DPnum];
                        binaryReader.Read(array2, 0, array2.Length);
                        binaryReader.BaseStream.Seek(4L, SeekOrigin.Current);
                        #endregion
                        if (Pnum >= 72006)
                        {
                            int Pnum4 = binaryReader.ReadInt32();
                            byte[] buffer = new byte[Pnum4];
                            fileStream.Read(buffer, 0, Pnum4);
                            using (MemoryStream stream = new MemoryStream(buffer))
                            {
                                DocumentImage = new Bitmap(stream);
                            }
                        }
                        else
                        {
                            fileStream.Seek(11L, SeekOrigin.Current);
                            int Pnum5 = binaryReader.ReadInt32();
                            long position = fileStream.Position;
                            int Pnum6 = binaryReader.ReadInt32();
                            int Pnum7 = Pnum5 - Pnum6;
                            int Pnum8 = 14 + Pnum6 + Pnum7;
                            fileStream.Position = position + 32;
                            int Pnum9 = 4 * binaryReader.ReadInt32();
                            if (Pnum9 == 0)
                            {
                                fileStream.Position = position + 14;
                                int Pnum10 = binaryReader.ReadInt16();
                                if (Pnum10 < 16)
                                {
                                    Pnum9 = 4 * (2 << Pnum10 - 1);
                                }
                            }
                            MemoryStream memoryStream = new MemoryStream(Pnum8);
                            using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
                            {
                                binaryWriter.Write((byte)66);
                                binaryWriter.Write((byte)77);
                                binaryWriter.Write(Pnum8);
                                binaryWriter.Write(0);
                                binaryWriter.Write(14 + Pnum6 + Pnum9);
                                byte[] buffer2 = new byte[Pnum5];
                                fileStream.Position = position;
                                fileStream.Read(buffer2, 0, Pnum5);
                                binaryWriter.Write(buffer2, 0, Pnum5);
                                memoryStream.Position = 0L;
                                DocumentImage = new Bitmap(memoryStream);
                            }
                        }

                    }
                }
            }
            catch (EndOfStreamException)
            {
                throw new FileFormatException("The end of the file was reached before a valid image was found. This is possibly due to the file not being a correctly formated BarTender format file.");
            }
            #endregion
            bool flag = !(backgroundColor == Color.Empty);
            if (backgroundColor == Color.Empty)
            {
                backgroundColor = DefaultBackground;
            }
            Bitmap bitmap = null;
            int thumbWidth = width;
            int num = DocumentImage.Height * width / DocumentImage.Width;
            if (num > height)
            {
                thumbWidth = DocumentImage.Width * height / DocumentImage.Height;
                num = height;
            }
            try
            {
                Image thumbnailImage = DocumentImage.GetThumbnailImage(thumbWidth, num, new Image.GetThumbnailImageAbort(() =>false), IntPtr.Zero);
                using (Bitmap bitmap2 = new Bitmap(thumbnailImage))
                {
                    if (flag)
                    {
                        bitmap2.MakeTransparent(DefaultBackground);
                    }
                    bitmap = new Bitmap(width, height);
                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
                        graphics.FillRectangle(new SolidBrush(backgroundColor), new Rectangle(0, 0, width, height));
                        int x = (width - bitmap2.Width) / 2;
                        int y = (height - bitmap2.Height) / 2;
                        graphics.DrawImage(bitmap2, new Point(x, y));
                        return bitmap;
                    }
                }
            }
            catch
            {
                Image thumbnailImage2 = DocumentImage.GetThumbnailImage(thumbWidth, num, new Image.GetThumbnailImageAbort(() => false), IntPtr.Zero);
                return new Bitmap(thumbnailImage2);
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值