• 2018年12月16日

C#将图片合成

        Bitmap bg = new Bitmap(500,500); //创建一个500x500px的画布
        bg.SetResolution(400, 400); //将画布设置为400x400分辨率
        Graphics g = Graphics.FromImage(bg); //新建画图对象
        g.Clear(Color.Transparent); //设置画图背景透明
        //将imgFile(通过FileDialog获取到的对象)画入画布并居中
        g.DrawImage(imgFile, (bg.Width - imgFile.Width) / 2, (bg.Height - imgFile.Height) / 2); //第二和三参数是置入图片在画布上的起始X和Y位置

 
        //设置文字
        System.Drawing.Font font = new System.Drawing.Font("思源黑体 CN", 12, FontStyle.Bold);  //设置字体
        SolidBrush brush = new SolidBrush(Color.White); //设置笔刷(写文字用)
        g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;//我选择这项,可以让写出来的文字没有黑边毛刺
        //写入文字
        g.DrawString(str, font, brush, xPosition, yPosition); //参数分别为写入的文字内容,字体,笔刷,x位置,y位置
        bg.Save(pngPath+"\\"+ no + ".png",ImageFormat.Png); //保存,参数分别为保存文件路径,图片格式
        brush.Dispose();
        font.Dispose();
        g.Dispose();
        img.Dispose();
        bg.Dispose();

说难也不难,希望对各位有用

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注