⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gdipimages.cs

📁 锤子大爷晓得锤子大爷晓得锤子大爷晓得锤子大爷晓得锤子大爷晓得锤子大爷晓得锤子大爷晓得
💻 CS
字号:
namespace GDIPlus.GDIPImages {
    using System;
    using System.Windows.Forms;
    using System.IO;
    using System.Net;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.Drawing.Text;
    using System.Reflection;

    public class ImagesSample : System.Windows.Forms.Form {
        /// <summary>
        ///    必需的设计器变量
        /// </summary>
        private System.ComponentModel.Container components;
        protected internal System.Windows.Forms.Button button1;

        private System.Drawing.Brush backgroundBrush;
        private System.Drawing.Image sample1;
        private System.Drawing.Image webLogo;
        private System.Drawing.Image createdImage;

        private FontFamily serifFontFamily;

        public ImagesSample() {

            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();

            serifFontFamily = new FontFamily (GenericFontFamilies.Serif);

            this.SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);

            Image backgroundImage;

            //从可执行文件的资源分支中加载要用于背景的图像
            backgroundImage = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("colorbars.jpg"));

            //现在创建要用于绘制背景的画笔
            backgroundBrush = new TextureBrush(backgroundImage);

            //现在加载要使用的其他图像
            sample1 = new Bitmap("sample.jpg");

            //从 Web 上加载图像并进行显示。如果失败,请从本地资源中加载图像
            try {
                WebRequest request = WebRequest.Create("http://www.microsoft.com/net/images/bnrWindowsNgws1.gif");
                request.Credentials = CredentialCache.DefaultCredentials;

                Stream source = request.GetResponse().GetResponseStream();
                MemoryStream ms = new MemoryStream();

                byte[] data = new byte[256];
                int c = source.Read(data, 0, data.Length);

                while (c > 0) {
                    ms.Write(data, 0, c);
                    c = source.Read(data, 0, data.Length);
                }

                source.Close();
                ms.Position = 0;
                webLogo = new Bitmap(ms);

            } catch(Exception) {
                // MessageBox.Show("未能从 Web 上加载图像 - 请使用默认图像\n\n " + ex.Message + " \n\n" + ex.StackTrace, "错误");
                webLogo = sample1;
            }

            //为呈现位图的按钮启动事件处理程序
            button1.Click += new System.EventHandler(DrawImage);

        }


        //按下按钮时激发
        private void DrawImage(object sender, EventArgs e) {

            Bitmap newBitmap = null;
            Graphics g = null ;

            try {
                newBitmap = new Bitmap(800,600,PixelFormat.Format32bppArgb);
                g = Graphics.FromImage(newBitmap);
                g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0,0,800,600));

                Font textFont = new Font(serifFontFamily, 12);
                RectangleF rectangle = new RectangleF(100, 100, 250, 350);
                string flowedText="我去了圣詹姆斯医院,\n到那里去看望我的爱人,\n她平躺在一张白色的桌子上,\n看上去是那么甜美、那么冰冷、那么美丽,\n让她去吧,让她去吧,愿上帝保佑她,\n不论她去哪里,\n她都可以俯瞰这个广阔的世界,\n但她永远不会找到一个像我这样的好男人,\n当我死时,希望你给我穿上整齐的系带鞋,\n我想穿一件直筒外套,戴一顶宽边帽,\n在我的表链上放上一个二十美元的金币,\n这样男孩们将知道我是站着死的。";

                g.FillRectangle(new SolidBrush(Color.Gainsboro), rectangle);
                g.DrawString(flowedText, textFont, new SolidBrush(Color.Blue), rectangle);
                Pen penExample = new Pen(Color.FromArgb(150, Color.Purple), 20);
                penExample.DashStyle = DashStyle.Dash;
                penExample.StartCap = LineCap.Round;
                penExample.EndCap = LineCap.Round;
                g.DrawCurve(penExample, new Point[] {
                            new Point(200, 140),
                            new Point(700, 240),
                            new Point(500, 340),
                            new Point(140, 140),
                            new Point(40, 340),
                            });

                newBitmap.Save("TestImage.png", ImageFormat.Png) ;

                MessageBox.Show("已完成 - 图像已写入 TestImage.png");

                //添加要绘制和重新绘制的图像
                createdImage = newBitmap;
                this.Invalidate();

            } finally {

                //注意:必须在处置该位图
                //之前处置图形对象

                //当不再需要所创建的
                //图形对象时要对其进行处置
                if (null != g) {
                    g.Dispose();
                }

                //通常在此处处置该位图
                //但因为我们打算在窗体
                //上显示它,所以先不做处置
                // if (null != newBitmap) {
                //     newBitmap.Dispose();
                // }
            }

        }


        protected override void OnPaint(PaintEventArgs e) {
            Graphics g = e.Graphics;

            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            //用粗画笔填充背景
            //然后应用白色涂料
            g.FillRectangle(backgroundBrush, ClientRectangle);
            g.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle);

            //只需呈现图像
            g.DrawImage(sample1, 29, 20, 283, 212);

            //现在旋转该图像并显示它
            g.RotateTransform(-30);
            g.DrawImage(webLogo, 175, 420);
            g.ResetTransform();

            //最后,对所创建的图像进行绘制(如果有该图像的话)
            if (createdImage != null) {
                g.DrawImage(createdImage, 50, 200, 200, 200);
            }
        }


        /// <summary>
        ///    清理正在使用的所有资源
        /// </summary>
        protected override void Dispose(bool disposing)
        {
           if (disposing) {
                if (components != null) {
                    components.Dispose();
                }
           }
           base.Dispose(disposing);
        }

        /// <summary>
        ///    设计器支持所必需的方法,不要使用
        ///    代码编辑器修改此方法的内容
        /// </summary>
        void InitializeComponent () {
            this.components = new System.ComponentModel.Container ();
            this.button1 = new System.Windows.Forms.Button ();
            this.Size = new System.Drawing.Size(750, 500);
            this.Text = "GDI+ 图像示例";
            button1.TabIndex = 0;
            button1.Text = "将位图绘制到文件";
            button1.Size = new System.Drawing.Size(100,50);
            button1.Location = new System.Drawing.Point(600,20);
            this.Controls.Add(button1);
        }

        /// <summary>
        /// 应用程序的主入口点
        /// </summary>
        [STAThread]
        public static void Main() {
            Application.Run(new ImagesSample());
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -