Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Set the NameSpace to project current NameSpace
- //Form Designer
- namespace BitmapFramesTest
- {
- partial class frmBitmapFrames
- {
- private System.ComponentModel.IContainer components = null;
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Windows Form Designer generated code
- private void InitializeComponent()
- {
- this.panScrollFrames = new System.Windows.Forms.Panel();
- this.pictureBox1 = new System.Windows.Forms.PictureBox();
- this.btnOpenFile = new System.Windows.Forms.Button();
- this.btnClear = new System.Windows.Forms.Button();
- this.rtbInfo = new System.Windows.Forms.RichTextBox();
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
- this.SuspendLayout();
- //
- // panScrollFrames
- //
- this.panScrollFrames.AutoScroll = true;
- this.panScrollFrames.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
- this.panScrollFrames.Dock = System.Windows.Forms.DockStyle.Bottom;
- this.panScrollFrames.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.panScrollFrames.ForeColor = System.Drawing.Color.Black;
- this.panScrollFrames.Location = new System.Drawing.Point(0, 245);
- this.panScrollFrames.Name = "panScrollFrames";
- this.panScrollFrames.Size = new System.Drawing.Size(415, 126);
- this.panScrollFrames.TabIndex = 0;
- //
- // pictureBox1
- //
- this.pictureBox1.Location = new System.Drawing.Point(183, 11);
- this.pictureBox1.Name = "pictureBox1";
- this.pictureBox1.Size = new System.Drawing.Size(220, 220);
- this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
- this.pictureBox1.TabIndex = 1;
- this.pictureBox1.TabStop = false;
- //
- // btnOpenFile
- //
- this.btnOpenFile.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
- this.btnOpenFile.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
- this.btnOpenFile.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
- this.btnOpenFile.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btnOpenFile.ForeColor = System.Drawing.Color.White;
- this.btnOpenFile.Location = new System.Drawing.Point(12, 12);
- this.btnOpenFile.Name = "btnOpenFile";
- this.btnOpenFile.Size = new System.Drawing.Size(106, 35);
- this.btnOpenFile.TabIndex = 2;
- this.btnOpenFile.Text = "Open Bitmap";
- this.btnOpenFile.UseVisualStyleBackColor = true;
- this.btnOpenFile.Click += new System.EventHandler(this.btnOpenFile_Click);
- //
- // btnClear
- //
- this.btnClear.FlatAppearance.MouseDownBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
- this.btnClear.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
- this.btnClear.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
- this.btnClear.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btnClear.ForeColor = System.Drawing.Color.White;
- this.btnClear.Location = new System.Drawing.Point(12, 53);
- this.btnClear.Name = "btnClear";
- this.btnClear.Size = new System.Drawing.Size(106, 35);
- this.btnClear.TabIndex = 3;
- this.btnClear.Text = "Clear ViewPort";
- this.btnClear.UseVisualStyleBackColor = true;
- this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
- //
- // rtbInfo
- //
- this.rtbInfo.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(32)))), ((int)(((byte)(32)))), ((int)(((byte)(32)))));
- this.rtbInfo.BorderStyle = System.Windows.Forms.BorderStyle.None;
- this.rtbInfo.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.rtbInfo.ForeColor = System.Drawing.Color.White;
- this.rtbInfo.Location = new System.Drawing.Point(12, 132);
- this.rtbInfo.Name = "rtbInfo";
- this.rtbInfo.Size = new System.Drawing.Size(165, 99);
- this.rtbInfo.TabIndex = 4;
- this.rtbInfo.Text = "";
- //
- // frmBitmapFrames
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(32)))), ((int)(((byte)(32)))), ((int)(((byte)(32)))));
- this.ClientSize = new System.Drawing.Size(415, 371);
- this.Controls.Add(this.rtbInfo);
- this.Controls.Add(this.btnClear);
- this.Controls.Add(this.btnOpenFile);
- this.Controls.Add(this.pictureBox1);
- this.Controls.Add(this.panScrollFrames);
- this.Name = "frmBitmapFrames";
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
- this.Text = "frmBitmapFrames";
- this.Load += new System.EventHandler(this.frmBitmapFrames_Load);
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
- this.ResumeLayout(false);
- }
- #endregion
- private System.Windows.Forms.Panel panScrollFrames;
- private System.Windows.Forms.PictureBox pictureBox1;
- private System.Windows.Forms.Button btnOpenFile;
- private System.Windows.Forms.Button btnClear;
- private System.Windows.Forms.RichTextBox rtbInfo;
- }
- }
- // Form code
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Windows.Media.Imaging;
- using System.Windows.Forms;
- namespace BitmapFramesTest
- {
- public partial class frmBitmapFrames : Form
- {
- internal BitmapDecoder Decoder = null;
- private List<PictureBox> ImageContainers = null;
- private List<BitmapFrame> Frames;
- private Bitmap CurrentBitmap = null;
- private int CurrentSelectedFrame = -1;
- private float DefaultFrameHeight = 0.0F;
- public frmBitmapFrames()
- {
- InitializeComponent();
- this.ImageContainers = new List<PictureBox>();
- this.Frames = new List<BitmapFrame>();
- DefaultFrameHeight = this.panScrollFrames.Height - 22;
- }
- private BitmapDecoder DecodeBitmap(string FileName)
- {
- using (FileStream stream = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.None))
- return DecodeBitmap(stream);
- }
- private BitmapDecoder DecodeBitmap(FileStream stream)
- {
- MemoryStream memstream = new MemoryStream();
- stream.CopyTo(memstream);
- memstream.Position = 0;
- BitmapDecoder bitmapDecoder = BitmapDecoder.Create(memstream,
- BitmapCreateOptions.PreservePixelFormat,
- BitmapCacheOption.Default);
- this.Frames = bitmapDecoder.Frames.ToList();
- return bitmapDecoder;
- }
- internal void BuildImageList()
- {
- SizeF ImageSize = CalculateImageSize(this.Decoder.Frames[0]);
- PointF ImagePosition = new PointF(0, 0);
- foreach (BitmapFrame frame in this.Decoder.Frames)
- {
- this.CurrentSelectedFrame += 1;
- this.ImageContainers.Add(new PictureBox()
- {
- Image = BitmapFromSource(frame),
- Size = System.Drawing.Size.Round(ImageSize),
- Location = System.Drawing.Point.Round(ImagePosition),
- SizeMode = PictureBoxSizeMode.Zoom,
- Tag = this.CurrentSelectedFrame
- });
- ImageContainers.Last().Click += new EventHandler(this.pictureBoxClick);
- ImagePosition.X += ImageSize.Width + 10;
- }
- this.panScrollFrames.Controls.AddRange(ImageContainers.ToArray());
- }
- public Bitmap BitmapFromSource(BitmapSource source)
- {
- using (MemoryStream outStream = new MemoryStream())
- {
- BitmapEncoder encoder = new BmpBitmapEncoder();
- encoder.Frames.Add(BitmapFrame.Create(source));
- encoder.Save(outStream);
- using (Bitmap bitmap = new Bitmap(outStream))
- return (Bitmap)bitmap.Clone();
- }
- }
- private SizeF CalculateImageSize(BitmapSource source)
- {
- float ScaleFactor = (float)source.Height / DefaultFrameHeight;
- return new SizeF(((float)source.Width / ScaleFactor), DefaultFrameHeight);
- }
- private void ClearVewport()
- {
- foreach (Control ctrl in this.panScrollFrames.Controls.OfType<PictureBox>())
- ctrl.Dispose();
- this.panScrollFrames.Controls.Clear();
- this.ImageContainers.Clear();
- this.rtbInfo.Text = string.Empty;
- this.Decoder = null;
- }
- protected void pictureBoxClick(object sender, EventArgs e)
- {
- this.CurrentBitmap = (Bitmap)((PictureBox)sender).Image.Clone();
- this.CurrentSelectedFrame = (int)((PictureBox)sender).Tag;
- //pictureBox1.Image = this.CurrentBitmap;
- pictureBox1.Image = BitmapFromSource(this.Frames[this.CurrentSelectedFrame]);
- }
- private void btnOpenFile_Click(object sender, EventArgs e)
- {
- using (OpenFileDialog ofd = new OpenFileDialog())
- {
- ofd.Filter = "All Files (*.*)|*.*";
- ofd.Multiselect = false;
- if (ofd.ShowDialog() == DialogResult.Cancel) return;
- if (this.ImageContainers.Count > 0)
- ClearVewport();
- this.Decoder = DecodeBitmap(ofd.FileName);
- }
- rtbInfo.AppendText($"Image Size: \t");
- rtbInfo.SelectionColor = System.Drawing.Color.LightGreen;
- rtbInfo.AppendText($"{this.Decoder.Frames[0].Width}x{this.Decoder.Frames[0].Height}\n");
- rtbInfo.AppendText($"Pixel Format: \t");
- rtbInfo.SelectionColor = System.Drawing.Color.LightGreen;
- rtbInfo.AppendText($"{this.Decoder.Frames[0].Format.ToString()}\n");
- rtbInfo.AppendText($"Frames Count: \t");
- rtbInfo.SelectionColor = System.Drawing.Color.LightGreen;
- rtbInfo.AppendText($"{this.Decoder.Frames.Count}\n");
- BuildImageList();
- }
- private void btnClear_Click(object sender, EventArgs e)
- {
- ClearVewport();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment