创建和使用位图和图标

如何:在运行时创建位图 (Visual C#)

此示例创建并填充 Bitmap 对象,然后在 PictureBox 控件中显示该对象。若要运行此示例,请创建一个 Windows 窗体应用程序项目,并将 PictureBox 控件从“工具箱”拖到窗体上。图片框的大小并不重要;它将自动调整自身大小以适合位图。将 CreateBitmap 方法粘贴到 Form1 类中,然后从 Form1_Load 事件处理程序方法中调用它。

void CreateBitmap()
{
  const int colWidth = 10;
   const int rowHeight = 10;
   System.Drawing.Bitmap checks = new System.Drawing.Bitmap(
       colWidth * 10, rowHeight * 10);

  // The checkerboard consists of 10 rows and 10 columns.
  // Each square in the checkerboard is 10 x 10 pixels.
  // The nested for loops are used to calculate the position
  // of each square on the bitmap surface, and to set the
  // pixels to black or white.

  // The two outer loops iterate through 
  //  each square in the bitmap surface.
  for (int columns = 0; columns < 10; columns++)
  {
     for (int rows = 0; rows < 10; rows++)
    {
       // Determine whether the current sqaure
       // should be black or white.
       Color color;
       if (columns % 2 == 0)
         color = rows % 2 == 0 ? Color.Black : Color.White;
       else
         color = rows % 2 == 0 ? Color.White : Color.Black;

    // The two inner loops iterate through
    // each pixel in an individual square.
    for (int j = columns * colWidth; j < (columns * colWidth) + colWidth; j++)
    {
    for (int k = rows * rowHeight; k < (rows * rowHeight) + rowHeight; k++)
    {
     // Set the pixel to the correct color.
     checks.SetPixel(j, k, color);
    }
    }
   }
  }
}
如何:将图像从一种格式转换为另一种格式

class Program
{
    static void Main(string[] args)
    {
        // Load the image.
        System.Drawing.Image image1 = System.Drawing.Image.FromFile(@"C:\test.bmp");

        // Save the image in JPEG format.
        image1.Save(@"C:\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

        // Save the image in GIF format.
        image1.Save(@"C:\test.gif", System.Drawing.Imaging.ImageFormat.Gif);

        // Save the image in PNG format.
        image1.Save(@"C:\test.png", System.Drawing.Imaging.ImageFormat.Png);        
    }
}

可以从命令提示符编译该示例,也可以使用 IDE 将代码粘贴到控制台应用程序中。在后一种情况中,必须引用 System.Drawing.dll 文件。

将 "c:\test.bmp", "c:\test.jpg", "c:\test.gif" and c:\test.png 替换为实际的文件名。

如何:检索作为嵌入资源的图像

本示例检索一个图像,该图像是程序集的嵌入资源。

设置此示例

  1. 创建一个 Windows 窗体应用程序,其中包含一个名为 pictureBox1 的 PictureBox 控件。

  2. 将下面的代码示例添加到 Form1_Load 事件处理程序中。

    System.Reflection.Assembly thisExe;
    thisExe = System.Reflection.Assembly.GetExecutingAssembly();
    System.IO.Stream file = 
        thisExe.GetManifestResourceStream("AssemblyName.ImageFile.jpg");
    this.pictureBox1.Image = Image.FromStream(file);
    
  3. 将一个现有图像文件添加到该项目中,并在“解决方案资源管理器”中将其“生成操作”属性设置为“嵌入的资源”

  4. 将 "AssemblyName.ImageFile.jpg" 替换为程序集中的资源的名称。

  5. 可以使用 Assembly 对象的 GetManifestResourceNames 方法查找该资源的名称。

以下情况可能会导致异常:

  • 嵌入的资源不在程序集中,因此调用 GetManifestResourceStream 将返回 Nothing

  • 该文件类型(即,文件扩展名)可能没有关联的应用程序。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值