Jimi2000

BitmapFramesTest

Jun 12th, 2018
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.79 KB | None | 0 0
  1. Set the NameSpace to project current NameSpace
  2.  
  3. //Form Designer
  4. namespace BitmapFramesTest
  5. {
  6.     partial class frmBitmapFrames
  7.     {
  8.         private System.ComponentModel.IContainer components = null;
  9.  
  10.         protected override void Dispose(bool disposing)
  11.         {
  12.             if (disposing && (components != null))
  13.             {
  14.                 components.Dispose();
  15.             }
  16.             base.Dispose(disposing);
  17.         }
  18.  
  19.         #region Windows Form Designer generated code
  20.  
  21.         private void InitializeComponent()
  22.         {
  23.             this.panScrollFrames = new System.Windows.Forms.Panel();
  24.             this.pictureBox1 = new System.Windows.Forms.PictureBox();
  25.             this.btnOpenFile = new System.Windows.Forms.Button();
  26.             this.btnClear = new System.Windows.Forms.Button();
  27.             this.rtbInfo = new System.Windows.Forms.RichTextBox();
  28.             ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
  29.             this.SuspendLayout();
  30.             //
  31.             // panScrollFrames
  32.             //
  33.             this.panScrollFrames.AutoScroll = true;
  34.             this.panScrollFrames.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  35.             this.panScrollFrames.Dock = System.Windows.Forms.DockStyle.Bottom;
  36.             this.panScrollFrames.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  37.             this.panScrollFrames.ForeColor = System.Drawing.Color.Black;
  38.             this.panScrollFrames.Location = new System.Drawing.Point(0, 245);
  39.             this.panScrollFrames.Name = "panScrollFrames";
  40.             this.panScrollFrames.Size = new System.Drawing.Size(415, 126);
  41.             this.panScrollFrames.TabIndex = 0;
  42.             //
  43.             // pictureBox1
  44.             //
  45.             this.pictureBox1.Location = new System.Drawing.Point(183, 11);
  46.             this.pictureBox1.Name = "pictureBox1";
  47.             this.pictureBox1.Size = new System.Drawing.Size(220, 220);
  48.             this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  49.             this.pictureBox1.TabIndex = 1;
  50.             this.pictureBox1.TabStop = false;
  51.             //
  52.             // btnOpenFile
  53.             //
  54.             this.btnOpenFile.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
  55.             this.btnOpenFile.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
  56.             this.btnOpenFile.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  57.             this.btnOpenFile.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  58.             this.btnOpenFile.ForeColor = System.Drawing.Color.White;
  59.             this.btnOpenFile.Location = new System.Drawing.Point(12, 12);
  60.             this.btnOpenFile.Name = "btnOpenFile";
  61.             this.btnOpenFile.Size = new System.Drawing.Size(106, 35);
  62.             this.btnOpenFile.TabIndex = 2;
  63.             this.btnOpenFile.Text = "Open Bitmap";
  64.             this.btnOpenFile.UseVisualStyleBackColor = true;
  65.             this.btnOpenFile.Click += new System.EventHandler(this.btnOpenFile_Click);
  66.             //
  67.             // btnClear
  68.             //
  69.             this.btnClear.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
  70.             this.btnClear.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
  71.             this.btnClear.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  72.             this.btnClear.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  73.             this.btnClear.ForeColor = System.Drawing.Color.White;
  74.             this.btnClear.Location = new System.Drawing.Point(12, 53);
  75.             this.btnClear.Name = "btnClear";
  76.             this.btnClear.Size = new System.Drawing.Size(106, 35);
  77.             this.btnClear.TabIndex = 3;
  78.             this.btnClear.Text = "Clear ViewPort";
  79.             this.btnClear.UseVisualStyleBackColor = true;
  80.             this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
  81.             //
  82.             // rtbInfo
  83.             //
  84.             this.rtbInfo.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(32)))), ((int)(((byte)(32)))), ((int)(((byte)(32)))));
  85.             this.rtbInfo.BorderStyle = System.Windows.Forms.BorderStyle.None;
  86.             this.rtbInfo.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  87.             this.rtbInfo.ForeColor = System.Drawing.Color.White;
  88.             this.rtbInfo.Location = new System.Drawing.Point(12, 132);
  89.             this.rtbInfo.Name = "rtbInfo";
  90.             this.rtbInfo.Size = new System.Drawing.Size(165, 99);
  91.             this.rtbInfo.TabIndex = 4;
  92.             this.rtbInfo.Text = "";
  93.             //
  94.             // frmBitmapFrames
  95.             //
  96.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  97.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  98.             this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(32)))), ((int)(((byte)(32)))), ((int)(((byte)(32)))));
  99.             this.ClientSize = new System.Drawing.Size(415, 371);
  100.             this.Controls.Add(this.rtbInfo);
  101.             this.Controls.Add(this.btnClear);
  102.             this.Controls.Add(this.btnOpenFile);
  103.             this.Controls.Add(this.pictureBox1);
  104.             this.Controls.Add(this.panScrollFrames);
  105.             this.Name = "frmBitmapFrames";
  106.             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  107.             this.Text = "frmBitmapFrames";
  108.             this.Load += new System.EventHandler(this.frmBitmapFrames_Load);
  109.             ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
  110.             this.ResumeLayout(false);
  111.  
  112.         }
  113.  
  114.         #endregion
  115.  
  116.         private System.Windows.Forms.Panel panScrollFrames;
  117.         private System.Windows.Forms.PictureBox pictureBox1;
  118.         private System.Windows.Forms.Button btnOpenFile;
  119.         private System.Windows.Forms.Button btnClear;
  120.         private System.Windows.Forms.RichTextBox rtbInfo;
  121.     }
  122. }
  123.  
  124.  
  125. // Form code
  126.  
  127. using System;
  128. using System.Collections.Generic;
  129. using System.Drawing;
  130. using System.IO;
  131. using System.Linq;
  132. using System.Text;
  133. using System.Windows.Media.Imaging;
  134. using System.Windows.Forms;
  135.  
  136. namespace BitmapFramesTest
  137. {
  138.     public partial class frmBitmapFrames : Form
  139.     {
  140.         internal BitmapDecoder Decoder = null;
  141.         private List<PictureBox> ImageContainers = null;
  142.         private List<BitmapFrame> Frames;
  143.         private Bitmap CurrentBitmap = null;
  144.         private int CurrentSelectedFrame = -1;
  145.         private float DefaultFrameHeight = 0.0F;
  146.  
  147.         public frmBitmapFrames()
  148.         {
  149.             InitializeComponent();
  150.             this.ImageContainers = new List<PictureBox>();
  151.             this.Frames = new List<BitmapFrame>();
  152.             DefaultFrameHeight = this.panScrollFrames.Height - 22;
  153.         }
  154.  
  155.         private BitmapDecoder DecodeBitmap(string FileName)
  156.         {
  157.             using (FileStream stream = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.None))
  158.                 return DecodeBitmap(stream);
  159.         }
  160.  
  161.         private BitmapDecoder DecodeBitmap(FileStream stream)
  162.         {
  163.             MemoryStream memstream = new MemoryStream();
  164.             stream.CopyTo(memstream);
  165.             memstream.Position = 0;
  166.             BitmapDecoder bitmapDecoder = BitmapDecoder.Create(memstream,
  167.                                                         BitmapCreateOptions.PreservePixelFormat,
  168.                                                         BitmapCacheOption.Default);
  169.             this.Frames = bitmapDecoder.Frames.ToList();
  170.             return bitmapDecoder;
  171.         }
  172.  
  173.         internal void BuildImageList()
  174.         {
  175.             SizeF ImageSize = CalculateImageSize(this.Decoder.Frames[0]);
  176.             PointF ImagePosition = new PointF(0, 0);
  177.             foreach (BitmapFrame frame in this.Decoder.Frames)
  178.             {
  179.                 this.CurrentSelectedFrame += 1;
  180.                 this.ImageContainers.Add(new PictureBox()
  181.                 {
  182.                     Image = BitmapFromSource(frame),
  183.                     Size = System.Drawing.Size.Round(ImageSize),
  184.                     Location = System.Drawing.Point.Round(ImagePosition),
  185.                     SizeMode = PictureBoxSizeMode.Zoom,
  186.                     Tag = this.CurrentSelectedFrame
  187.                 });
  188.                 ImageContainers.Last().Click += new EventHandler(this.pictureBoxClick);
  189.                 ImagePosition.X += ImageSize.Width + 10;
  190.             }
  191.             this.panScrollFrames.Controls.AddRange(ImageContainers.ToArray());
  192.         }
  193.  
  194.         public Bitmap BitmapFromSource(BitmapSource source)
  195.         {
  196.             using (MemoryStream outStream = new MemoryStream())
  197.             {
  198.                 BitmapEncoder encoder = new BmpBitmapEncoder();
  199.                 encoder.Frames.Add(BitmapFrame.Create(source));
  200.                 encoder.Save(outStream);
  201.                 using (Bitmap bitmap = new Bitmap(outStream))
  202.                     return (Bitmap)bitmap.Clone();
  203.             }
  204.         }
  205.  
  206.         private SizeF CalculateImageSize(BitmapSource source)
  207.         {
  208.             float ScaleFactor = (float)source.Height / DefaultFrameHeight;
  209.             return new SizeF(((float)source.Width / ScaleFactor), DefaultFrameHeight);
  210.         }
  211.  
  212.         private void ClearVewport()
  213.         {
  214.             foreach (Control ctrl in this.panScrollFrames.Controls.OfType<PictureBox>())
  215.                 ctrl.Dispose();
  216.             this.panScrollFrames.Controls.Clear();
  217.             this.ImageContainers.Clear();
  218.             this.rtbInfo.Text = string.Empty;
  219.             this.Decoder = null;
  220.         }
  221.  
  222.         protected void pictureBoxClick(object sender, EventArgs e)
  223.         {
  224.             this.CurrentBitmap = (Bitmap)((PictureBox)sender).Image.Clone();
  225.             this.CurrentSelectedFrame = (int)((PictureBox)sender).Tag;
  226.             //pictureBox1.Image = this.CurrentBitmap;
  227.             pictureBox1.Image = BitmapFromSource(this.Frames[this.CurrentSelectedFrame]);
  228.         }
  229.  
  230.         private void btnOpenFile_Click(object sender, EventArgs e)
  231.         {
  232.             using (OpenFileDialog ofd = new OpenFileDialog())
  233.             {
  234.                 ofd.Filter = "All Files (*.*)|*.*";
  235.                 ofd.Multiselect = false;
  236.                 if (ofd.ShowDialog() == DialogResult.Cancel) return;
  237.  
  238.                 if (this.ImageContainers.Count > 0)
  239.                     ClearVewport();
  240.                 this.Decoder = DecodeBitmap(ofd.FileName);
  241.             }
  242.             rtbInfo.AppendText($"Image Size: \t");
  243.             rtbInfo.SelectionColor = System.Drawing.Color.LightGreen;
  244.             rtbInfo.AppendText($"{this.Decoder.Frames[0].Width}x{this.Decoder.Frames[0].Height}\n");
  245.             rtbInfo.AppendText($"Pixel Format: \t");
  246.             rtbInfo.SelectionColor = System.Drawing.Color.LightGreen;
  247.             rtbInfo.AppendText($"{this.Decoder.Frames[0].Format.ToString()}\n");
  248.             rtbInfo.AppendText($"Frames Count: \t");
  249.             rtbInfo.SelectionColor = System.Drawing.Color.LightGreen;
  250.             rtbInfo.AppendText($"{this.Decoder.Frames.Count}\n");
  251.             BuildImageList();
  252.         }
  253.  
  254.         private void btnClear_Click(object sender, EventArgs e)
  255.         {
  256.             ClearVewport();
  257.         }
  258.     }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment