zoukankan      html  css  js  c++  java
  • csharp .net vb 复制图像

    .NET Compact Framework 不支持 Image.Clone 方法,可是仍能够复制图像和图像的某些部分。以下的演示例子演示怎样运行以下操作:

    • 定义一个方法以创建位图。

    • 定义一个重载方法以复制位图或位图的一部分。

    • 通过重写窗口的 OnPaint 方法来调用这些方法并向屏幕绘制图像。

    创建位图

    • 此方法创建一个位图以进行演示。

     
    // Creates a bitmap for copying.
    private Bitmap CreateBitmap(int sideSize)
    {
        Bitmap bmp = new Bitmap(sideSize, sideSize);
        Graphics g = Graphics.FromImage(bmp);
    
        g.FillEllipse(new SolidBrush(Color.Red), 0, 0, sideSize, sideSize);
        g.DrawLine(new Pen(Color.Black), 0, 0, sideSize, sideSize);
        g.DrawLine(new Pen(Color.Black), sideSize, 0, 0, sideSize);
        g.Dispose();
    
        return bmp;
    }
    
    
    

    克隆位图

    • 此方法重载採用源位图作为參数并将该位图作为副本返回。

     
    // Copies the entire bitmap.
    protected Bitmap CopyBitmap(Bitmap source)
    {
        return new Bitmap(source);
    }
    
    
    

    复制位图的一部分

    • 此方法重载採用 Rectangle 作为參数以确定要返回的位图部分的尺寸。

     
    // Copies a part of a bitmap.
    protected Bitmap CopyBitmap(Bitmap source, Rectangle part)
    {
        Bitmap bmp = new Bitmap(part.Width, part.Height);
        Graphics g = Graphics.FromImage(bmp);
        g.DrawImage(source,0,0,part,GraphicsUnit.Pixel);
        g.Dispose();
        return bmp;
    }
    
    
    

    创建、复制和绘制位图

    • 此 OnPaint 方法重载调用方法创建一个位图,然后克隆并复制该位图的一部分。此方法也能够将克隆的位图保存到一个文件里。

     
    // Draws the bitmaps on the form.   
    protected override void OnPaint(PaintEventArgs e)
    {
        Font arialFont;
        Brush blackBrush;
        arialFont = new Font("Arial", 10, FontStyle.Regular);
        blackBrush = new SolidBrush(Color.Black);
    
        // Set the size of the sides of the bitmap,
        // and get one-third of it for the center bitmap.
        int sidesize = 75;
        int third = (int) sidesize/3;
    
        // Create bitmap.
        source = CreateBitmap(sidesize);
    
        // Copy entirely as a clone.
        clone = CopyBitmap(source);
    
        // Copy the center part of the bitmap.
        center = CopyBitmap(source, new Rectangle(third, third, third, third));
    
        // Save the bitmap to a file.
        clone.Save("newbitmap.bmp", ImageFormat.Bmp);
    
        // Draw the source, clone, and partial 
        // bitmaps vertically down the screen. 
        int y = 10;
    
        e.Graphics.DrawString("source bitmap:", arialFont, blackBrush, 10, y);
        y += 20;
    
        e.Graphics.DrawImage(source, 10, y);
        y += source.Height + 10;
    
        e.Graphics.DrawString("clone bitmap:", arialFont, blackBrush, 10, y);
        y += 20;
    
        e.Graphics.DrawImage(clone, 10, y);
        y += clone.Height + 10;
    
        e.Graphics.DrawString("center part of bitmap:", arialFont, blackBrush, 10, y);
        y += 20;
    
        e.Graphics.DrawImage(center, 10, y);
        y += center.Height + 10;
    
        // Dispose graphic objects.
        arialFont.Dispose();
        blackBrush.Dispose();
    }
    
    
    

    此演示例子须要引用以下的命名空间:

    注意,Font 和 Brush 对象在 OnPaint 方法重载中显式释放。由 PaintEventArgs 对象的 Graphics 属性返回的 Graphics 对象将由垃圾回收器销毁,不须要显式释放。

  • 相关阅读:
    洛谷.3254.圆桌问题(最大流ISAP)
    BZOJ.2639.矩形计算(二维莫队)
    BZOJ.2724.[Violet 6]蒲公英(静态分块)
    BZOJ.4241.历史研究(回滚莫队 分块)
    BZOJ.4542.[HNOI2016]大数(莫队)
    SPOJ.COT2 Count on a tree II(树上莫队)
    BZOJ.3757.苹果树(树上莫队)
    洛谷.2325.[SCOI2005]王室联邦(贪心)
    BZOJ.1299.[LLH邀请赛]巧克力棒(博弈论 Nim)
    HDU.2516.取石子游戏(博弈论 Fibonacci Nim)
  • 原文地址:https://www.cnblogs.com/mfryf/p/3622331.html
Copyright © 2011-2022 走看看