成人欧美一区二区三区的电影,日韩一级一欧美一级国产,国产成人国拍亚洲精品,无码人妻精品一区二区三区毛片,伊人久久无码大香线蕉综合

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#造個(gè)輪子--GIF錄制工具

freeflydom
2025年10月9日 9:33 本文熱度 337

        在以往幾篇文章里面,大家都可以看到各種錄制的GIF效果圖,把gif放在文章開(kāi)始,不僅可以減少很多冗余的解釋白話文,更可以讓讀者一覽無(wú)余看到文章大概要義。

以往都是使用“LicEcap”來(lái)錄制的,那么我們是否能自己實(shí)現(xiàn)一個(gè)這樣的工具呢?一方面國(guó)慶假期結(jié)束,練練代碼手感,另一方面可以根據(jù)自己需求擴(kuò)展需要的功能。

 

介紹軟件UI及操作

操作比較簡(jiǎn)單,以下是運(yùn)行界面:

  1. 選擇錄制區(qū)域,繪制需要錄制的ROI區(qū)域 

  2. 點(diǎn)擊開(kāi)始錄制

  3. 錄制結(jié)束后,停止錄制即可.彈出保存路徑保存gif

 

源碼介紹

private void InitializeComponents()
 {
     this.Text = "GIF錄制工具";
     this.Size = new Size(400, 200);
     this.StartPosition = FormStartPosition.CenterScreen;
     // 選擇區(qū)域按鈕
     Button btnSelectArea = new Button();
     btnSelectArea.Text = "選擇錄制區(qū)域";
     btnSelectArea.Size = new Size(120, 30);
     btnSelectArea.Location = new Point(20, 20);
     btnSelectArea.Click += BtnSelectArea_Click;
     this.Controls.Add(btnSelectArea);
     // 開(kāi)始錄制按鈕
     Button btnStart = new Button();
     btnStart.Text = "開(kāi)始錄制";
     btnStart.Size = new Size(120, 30);
     btnStart.Location = new Point(20, 60);
     btnStart.Click += BtnStart_Click;
     this.Controls.Add(btnStart);
     // 停止錄制按鈕
     Button btnStop = new Button();
     btnStop.Text = "停止錄制";
     btnStop.Size = new Size(120, 30);
     btnStop.Location = new Point(20, 100);
     btnStop.Click += BtnStop_Click;
     this.Controls.Add(btnStop);
     // 幀率選擇
     Label lblFrameRate = new Label();
     lblFrameRate.Text = "幀率:";
     lblFrameRate.Location = new Point(160, 65);
     lblFrameRate.Size = new Size(50, 20);
     this.Controls.Add(lblFrameRate);
     NumericUpDown numFrameRate = new NumericUpDown();
     numFrameRate.Value = frameRate;
     numFrameRate.Minimum = 1;
     numFrameRate.Maximum = 30;
     numFrameRate.Location = new Point(210, 65);
     numFrameRate.Size = new Size(60, 20);
     numFrameRate.ValueChanged += (s, e) => { frameRate = (int)numFrameRate.Value; };
     this.Controls.Add(numFrameRate);
     // 狀態(tài)標(biāo)簽
     Label lblStatus = new Label();
     lblStatus.Text = "狀態(tài): 就緒";
     lblStatus.Location = new Point(160, 25);
     lblStatus.Size = new Size(200, 20);
     lblStatus.Name = "lblStatus";
     this.Controls.Add(lblStatus);
     // 錄制計(jì)時(shí)器
     captureTimer = new System.Windows.Forms.Timer();
     captureTimer.Tick += CaptureTimer_Tick;
 }

選擇ROI錄屏區(qū)域

private void StartAreaSelection()
 {
     this.Hide();
     Thread.Sleep(500); // 等待窗體隱藏
     isSelectingArea = true;
     Cursor = Cursors.Cross;
     // 創(chuàng)建全屏透明窗體用于區(qū)域選擇
     Form selectionForm = new Form();
     selectionForm.WindowState = FormWindowState.Maximized;
     selectionForm.FormBorderStyle = FormBorderStyle.None;
     selectionForm.BackColor = Color.Black;
     selectionForm.Opacity = 0.3;
     selectionForm.TopMost = true;
     selectionForm.Cursor = Cursors.Cross;
     Rectangle selectedArea = Rectangle.Empty;
     bool isDragging = false;
     Point dragStart = Point.Empty;
     selectionForm.MouseDown += (s, e) =>
     {
         if (e.Button == MouseButtons.Left)
         {
             isDragging = true;
             dragStart = e.Location;
         }
     };
     selectionForm.MouseMove += (s, e) =>
     {
         if (isDragging)
         {
             int x = Math.Min(dragStart.X, e.X);
             int y = Math.Min(dragStart.Y, e.Y);
             int width = Math.Abs(e.X - dragStart.X);
             int height = Math.Abs(e.Y - dragStart.Y);
             selectedArea = new Rectangle(x, y, width, height);
             selectionForm.Invalidate();
         }
     };
     selectionForm.MouseUp += (s, e) =>
     {
         if (e.Button == MouseButtons.Left && isDragging)
         {
             isDragging = false;
             if (selectedArea.Width > 10 && selectedArea.Height > 10)
             {
                 recordingArea = selectedArea;
                 UpdateStatus($"已選擇區(qū)域: {recordingArea}");
             }
             selectionForm.Close();
         }
     };
     selectionForm.Paint += (s, e) =>
     {
         if (isDragging && !selectedArea.IsEmpty)
         {
             using (Pen pen = new Pen(Color.Red, 2))
             {
                 e.Graphics.DrawRectangle(pen, selectedArea);
             }
             string sizeText = $"{selectedArea.Width} x {selectedArea.Height}";
             using (Font font = new Font("Arial", 12))
             using (Brush brush = new SolidBrush(Color.Red))
             {
                 e.Graphics.DrawString(sizeText, font, brush, selectedArea.X, selectedArea.Y - 20);
             }
         }
     };
     selectionForm.KeyDown += (s, e) =>
     {
         if (e.KeyCode == Keys.Escape)
         {
             selectionForm.Close();
         }
     };
     selectionForm.FormClosed += (s, e) =>
     {
         isSelectingArea = false;
         Cursor = Cursors.Default;
         this.Show();
         this.BringToFront();
     };
     selectionForm.ShowDialog();
 }

錄制結(jié)束,保存GIF

private void SaveGif()
  {
      if (frames.Count == 0)
      {
          MessageBox.Show("沒(méi)有可保存的幀!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
          return;
      }
      using (SaveFileDialog saveDialog = new SaveFileDialog())
      {
          saveDialog.Filter = "GIF 文件|*.gif";
          saveDialog.Title = "保存GIF文件";
          saveDialog.DefaultExt = "gif";
          if (saveDialog.ShowDialog() == DialogResult.OK)
          {
              try
              {
                  // 使用GifBitmapEncoder替代方案
                  SaveFramesAsGif(frames, saveDialog.FileName, frameRate);
                  MessageBox.Show($"GIF保存成功!\n文件: {saveDialog.FileName}\n幀數(shù): {frames.Count}", "成功",
                      MessageBoxButtons.OK, MessageBoxIcon.Information);
              }
              catch (Exception ex)
              {
                  MessageBox.Show($"保存GIF時(shí)出錯(cuò): {ex.Message}", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
              }
          }
      }
      // 清理資源
      foreach (var frame in frames)
      {
          frame.Dispose();
      }
      frames.Clear();
  }
private void SaveFramesAsGif(List<Bitmap> frames, string filePath, int frameRate)
  {
      using (var collection = new MagickImageCollection())
      {
          foreach (var frame in frames)
          {
              using (var memoryStream = new MemoryStream())
              {
                  frame.Save(memoryStream, ImageFormat.Bmp);
                  memoryStream.Position = 0;
                  var image = new MagickImage(memoryStream);
                  image.AnimationDelay =Convert.ToUInt32( 100 / frameRate); // 設(shè)置幀延遲
                  collection.Add(image);
              }
          }
          // 優(yōu)化GIF
          collection.Optimize();
          collection.Write(filePath);
      }
  }

主要用到第三方nuget包

  1. AnimatedGif

  2. Magick.NET-Q16-AnyCPU

轉(zhuǎn)自https://www.cnblogs.com/axing/p/19128750


該文章在 2025/10/9 9:33:56 編輯過(guò)
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved