Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 图形类:如何在图像的顶部、左侧、右侧和底部创建边距_C# - Fatal编程技术网

C# 图形类:如何在图像的顶部、左侧、右侧和底部创建边距

C# 图形类:如何在图像的顶部、左侧、右侧和底部创建边距,c#,C#,在ImageMagick上,我们可以使用-splice选项和-gravity选项在图像的顶部、左侧、右侧和底部创建边距 我想使用C#Graphics类在图像的顶部、左侧、右侧和底部创建边距。 但我不知道如何使用C#class创建边距,启用C#class创建边距 因此,我想知道上面提到的内容。您不想在C#graphics类上创建边距 图形对象是“能够被绘制的东西”的抽象。它可以是屏幕、打印机或位图 无法调整位图的大小。您必须创建一个新的,并将现有的复制到其中 因此,您需要做的是创建一个新位图,它是

在ImageMagick上,我们可以使用-splice选项和-gravity选项在图像的顶部、左侧、右侧和底部创建边距

我想使用C#Graphics类在图像的顶部、左侧、右侧和底部创建边距。

但我不知道如何使用C#class创建边距,启用C#class创建边距


因此,我想知道上面提到的内容。

您不想在C#graphics类上创建边距

图形对象是“能够被绘制的东西”的抽象。它可以是屏幕、打印机或位图

无法调整位图的大小。您必须创建一个新的,并将现有的复制到其中

因此,您需要做的是创建一个新位图,它是现有位图的副本,但周围有一个边距,然后使用图形对象将位图复制到它上

所以你需要

  • 创建一个足够大的位图,使其周围有位图和边距。使用“宽度”和“高度”属性查找现有位图的大小
  • 创建允许您在位图上绘制的图形对象(检查构造函数重载)
  • 然后使用图形对象将旧位图复制到新位图中。(查看DrawImage方法)
  • 最后处理图形对象,并以所需格式保存位图

    • 您不希望在C#graphics类上创建边距

      图形对象是“能够被绘制的东西”的抽象。它可以是屏幕、打印机或位图

      无法调整位图的大小。您必须创建一个新的,并将现有的复制到其中

      因此,您需要做的是创建一个新位图,它是现有位图的副本,但周围有一个边距,然后使用图形对象将位图复制到它上

      所以你需要

      • 创建一个足够大的位图,使其周围有位图和边距。使用“宽度”和“高度”属性查找现有位图的大小
      • 创建允许您在位图上绘制的图形对象(检查构造函数重载)
      • 然后使用图形对象将旧位图复制到新位图中。(查看DrawImage方法)
      • 最后处理图形对象,并以所需格式保存位图

      通常您是在System.Windows.Forms.Control/Form实例本身的属性中定义边距。请参见VisualStudio的设计者。并且-如果需要在控件的OnPaint方法或paint事件中自己绘制控件,可以尝试以下操作之一

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      
      namespace WindowsFormsApplication1
      {
          public partial class Form1 : Form
          {
              static readonly Bitmap image = Properties.Resources.gecco_quad_dunkel;
      
              public Form1()
              {
                  InitializeComponent();
              }
      
              private void panel1_Paint(object sender, PaintEventArgs e)
              {
                  // using my own margin
                  const int margin = 20;
      
                  var dest = new Rectangle(
                      e.ClipRectangle.X + margin, 
                      e.ClipRectangle.Y + margin, 
                      e.ClipRectangle.Width - 2 * margin, 
                      e.ClipRectangle.Height - 2 * margin
                      );
      
                  e.Graphics.DrawImage(image, dest);
              }
      
              private void panel2_Paint(object sender, PaintEventArgs e)
              {
                  // using the margin information of the System.Windows.Forms.Control/Form
      
                  var co = (Control)sender;
                  var dest = new Rectangle(
                      e.ClipRectangle.X + co.Margin.Left, 
                      e.ClipRectangle.Y + co.Margin.Top, 
                      e.ClipRectangle.Width - co.Margin.Left - co.Margin.Right, 
                      e.ClipRectangle.Height - co.Margin.Top - co.Margin.Bottom
                      );
      
                  e.Graphics.DrawImage(image, dest);
              }
          }
      }
      
      在我的表格中,我添加了两个绿色和橙色的容器(面板)。橙色的一个在所有边上都有20像素的边距。

      通常您是在System.Windows.Forms.Control/Form实例本身的属性中定义边距。请参见VisualStudio的设计者。并且-如果需要在控件的OnPaint方法或paint事件中自己绘制控件,可以尝试以下操作之一

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      
      namespace WindowsFormsApplication1
      {
          public partial class Form1 : Form
          {
              static readonly Bitmap image = Properties.Resources.gecco_quad_dunkel;
      
              public Form1()
              {
                  InitializeComponent();
              }
      
              private void panel1_Paint(object sender, PaintEventArgs e)
              {
                  // using my own margin
                  const int margin = 20;
      
                  var dest = new Rectangle(
                      e.ClipRectangle.X + margin, 
                      e.ClipRectangle.Y + margin, 
                      e.ClipRectangle.Width - 2 * margin, 
                      e.ClipRectangle.Height - 2 * margin
                      );
      
                  e.Graphics.DrawImage(image, dest);
              }
      
              private void panel2_Paint(object sender, PaintEventArgs e)
              {
                  // using the margin information of the System.Windows.Forms.Control/Form
      
                  var co = (Control)sender;
                  var dest = new Rectangle(
                      e.ClipRectangle.X + co.Margin.Left, 
                      e.ClipRectangle.Y + co.Margin.Top, 
                      e.ClipRectangle.Width - co.Margin.Left - co.Margin.Right, 
                      e.ClipRectangle.Height - co.Margin.Top - co.Margin.Bottom
                      );
      
                  e.Graphics.DrawImage(image, dest);
              }
          }
      }
      
      在我的表格中,我添加了两个绿色和橙色的容器(面板)。橙色的一个在所有边上都有20像素的边距。

      到目前为止,您尝试了什么?如果你向我们展示你的代码,以及你在哪里遇到困难,那么我们可以帮助你。也许你应该考虑使用它,它是一个C图像包装器。如果你向我们展示你的代码,以及你在哪里遇到困难,那么我们可以帮助你。也许你应该考虑使用它,它是一个用于IVEMAGEK的C包装。