CSDN文章的分享海报功能,用你熟悉的开发语言,挑战一下实现这一功能你用了多少时间

文章目录

效果
前言
实现步骤
实现分析
总结

效果

csdn里的效果

csdn文章里分享二维码
在这里插入图片描述

小5用C#语言实现的效果

在这里插入图片描述

前言

       有时候提高自身技术以及验证基础掌握扎实程度,就可以模仿各大互联网大佬的一些功能,比如,本次模仿的一个扫一扫二维码跳转页面生成一个分享海报的功能,小5用自己擅长的C#开发语言,小小实现一下,当然,各位同行也可以使用自己的开发语言挑战下,检验下自己完成需要多少时间以及用到了那些功能和插件

实现步骤

1)创建一张背景图图片,蓝色背景,高宽大小-1120x630
2)logo图片 - 透明遮罩,高宽度-100x630
3)logo图片
4)标题
5)圆角边框
6)左边小半圆圈边框
7)左边小圆圈背景
8)右边小半圆圈边框
9)右边小圆圈背景
10)边框内画虚线
11)描述内容
12)头像图片
13)昵称
14)底部黑色半透明矩形遮罩图片
15)大牛邀请你看优质博文

实现分析

注意文本和图片之间的叠加顺序,不要互相被覆盖了
1)创建一张背景图图片,蓝色背景,高宽大小-1120x630

//【1】创建一张背景图图片,蓝色背景,高宽大小-1120x630
Image backgroundImage;
string backgroundColor = "#099dff";
Color textColor = Color.White;

if (true)
{
    
    
    int backgroundWidth = 630;
    int backgroundHeight = 1120;
    backgroundImage = new Bitmap(backgroundWidth, backgroundHeight);
    using (Graphics graphics = Graphics.FromImage(backgroundImage))
    {
    
    
        //用笔刷画上蓝色背景
        Color bg = ColorTranslator.FromHtml(backgroundColor);
        graphics.FillRectangle(new SolidBrush(bg), new Rectangle(0, 0, backgroundImage.Width, backgroundImage.Height));
    }

    //save(backgroundImage);
}

2)logo图片 - 透明遮罩,高宽度-100x630

if (true)
{
    
    
    //创建高宽度为100x630的矩形图片
    Image maskImage = new Bitmap(630, 100);
    using (Graphics graphics = Graphics.FromImage(maskImage))
    {
    
    
        Color bg = Color.FromArgb(160, ColorTranslator.FromHtml("#0bbdff")); //设置一定值的透明度 0~255
        graphics.FillRectangle(new SolidBrush(bg), new Rectangle(0, 0, maskImage.Width, maskImage.Height));
    }

    //矩形图片叠加在背景图上
    using (Graphics graphics = Graphics.FromImage(backgroundImage))
    {
    
    
        Rectangle _r = new Rectangle(0, 0, maskImage.Width, maskImage.Height); //这里的坐标:叠图相对于底图的坐标,这里的大小进行等比设置
        graphics.DrawImage(maskImage, _r, 0, 0, maskImage.Width, maskImage.Height, GraphicsUnit.Pixel); //坐标:相对自己图片的坐标
    }

    //save(_backgroundImage);
}

3)logo图片
在这里插入图片描述

if (true)
{
    
    
    Image logoImage = new Bitmap(本地图片路径);

    int height = 100;
    int width = height * logoImage.Width / logoImage.Height;

    using (Graphics graphics = Graphics.FromImage(backgroundImage))
    {
    
    
        int logoImageInBackgroundImage_x = 30;
        int logoImageInBackgroundImage_y = 10; //这里的高宽度就可以对原图进行放大缩小显示
        Rectangle _r = new Rectangle(logoImageInBackgroundImage_x, logoImageInBackgroundImage_y, width, height); //叠图相对于底图的坐标

        int logoImageSelf_x = 0; //相对叠图自己的坐标系x轴,假设宽度为50,那么设置x=50,那么相对于自己的体系,就超出了自己的宽度,就看不见了
        int logoImageSelf_y = 0; //相对叠图自己的坐标系y轴
        graphics.DrawImage(logoImage, _r, logoImageSelf_x, logoImageSelf_y, logoImage.Width, logoImage.Height, GraphicsUnit.Pixel);
    }
}

4)标题
       文本在不同高宽度背景和字体大小,所呈现的文本宽高度都不一样,甚至在高宽背景图片和字体大小相同情况下,位深度不同都会有差别,所以,这个就需要在固定大小字体的前提下,计算一行大概占多少宽度,这样就可以计算出一行需要显示多个字,因为不同的字宽度还是有差异,所以计算宽度就比较合适了

if (true)
{
    
    
    FontStyle fontStyle = FontStyle.Regular;
    int fontSize = 25;
    string fontFamily = "黑体";

    string title = "CSDN文章的分享海报功能,用你熟悉的开发语言,挑战一下实现这一功能你用了多少时间";
    string displayTitle = "", displayTitleTwo = "";
    float maxTextWidth = 630 - 40 * 2;
    float tempWidth = 0;
    foreach(char item in title)
    {
    
    
        using (Graphics g = Graphics.FromImage(backgroundImage))
        {
    
    
            g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿

            using (Font f = new Font(fontFamily, fontSize, fontStyle))
            {
    
    
                using (Brush b = new SolidBrush(textColor))
                {
    
    
                    SizeF size = g.MeasureString(item.ToString(), new Font(fontFamily, fontSize, fontStyle));

                    if(tempWidth> maxTextWidth)
                    {
    
    
                        break;
                    }

                    displayTitle += item.ToString();
                    tempWidth += size.Width;
                }
            }
        }
    }
    if (title.Length > 15)
    {
    
    
        displayTitleTwo = title.Substring(displayTitle.Length, 15 - displayTitle.Length) + "...";
    }

    //标题第一行
    using (Graphics g = Graphics.FromImage(backgroundImage))
    {
    
    
        g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
        using (Font f = new Font(fontFamily, fontSize + 10, fontStyle))
        {
    
    
            using (Brush b = new SolidBrush(textColor))
            {
    
    
                SizeF size = g.MeasureString(displayTitle, new Font(fontFamily, fontSize + 10, fontStyle));
                float x = 40; //(backgroundImage.Width - size.Width) / 2; //水平居中
                float y = 180; //可以通过占比来大概计算位置=(88/545)*1120=180
                g.DrawString(displayTitle, f, b, x, y);
            }
        }
    }

    //标题第二行
    using (Graphics g = Graphics.FromImage(backgroundImage))
    {
    
    
        g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
        using (Font f = new Font(fontFamily, fontSize + 10, fontStyle))
        {
    
    
            using (Brush b = new SolidBrush(textColor))
            {
    
    
                SizeF size = g.MeasureString(displayTitleTwo, new Font(fontFamily, fontSize + 10, fontStyle));
                float x = 40; //(backgroundImage.Width - size.Width) / 2; //水平居中
                float y = 180+ size.Height; //可以通过占比来大概计算位置=(88/545)*1120=180
                g.DrawString(displayTitleTwo, f, b, x, y);
            }
        }
    }
}

5)圆角边框
       这里矩形圆角边框内的颜色,其实也是用了一个矩形填充颜色放到边框合适位置,看起来就感觉是矩形圆角边框填充的颜色。为什么要这样设置呢?因为能力水平有限,暂时没找到一个好的方法,就这样笨拙的实现下,哈哈哈!突然想到一个代替的解决方法,就是创建两个圆角矩形,A为白色,B为蓝色,B叠加在A上,同时,B的高宽度减少6的值,然后绝居中居中叠加在A上,这样就会出现白色区域,有种边框的感觉!后面二维码白边就是这样实现的。在这里插入图片描述

if (true)
{
    
    
    //先创建高宽510x630的图片,填充上#0aadff颜色背景
    int backgroundImage_x = 40;
    int backgroundImage_y = 350;

    int backgroundImageWidth = 550;
    int backgroundImageHeight = 510;

    Image borderImage = new Bitmap(backgroundImageWidth, backgroundImageHeight);

    using (Graphics g = Graphics.FromImage(borderImage))
    {
    
    
        //用笔刷画上蓝色背景
        Color bg = ColorTranslator.FromHtml("#0aadff");
        g.FillRectangle(new SolidBrush(bg), new Rectangle(0, 0, borderImage.Width, borderImage.Height));
    }

    //设置透明圆角的同时叠加在背景图片上
    using (Graphics g = Graphics.FromImage(backgroundImage))
    {
    
    
        //---透明圆角边框---
        int Radius = 20;
        Bitmap bitmap = new Bitmap(borderImage, borderImage.Width, borderImage.Height);

        using (Graphics bitmapGraphics = Graphics.FromImage(bitmap))
        {
    
    
            using(Brush brush = new System.Drawing.SolidBrush(Color.Red))
            {
    
    
                for (int i = 0; i < 4; i++)
                {
    
    
                    List<Point> point = new List<Point>();
                    point.Add(new Point() {
    
     X = 0, Y = 0 });
                    point.Add(new Point() {
    
     X = Radius, Y = 0 });
                    point.Add(new Point() {
    
     X = 0, Y = Radius });

                    System.Drawing.Drawing2D.GraphicsPath graphicsPath = new System.Drawing.Drawing2D.GraphicsPath();
                    graphicsPath.AddArc(point[0].X, point[0].Y, Radius, Radius, 180, 90);
                    graphicsPath.AddLine(point[0].X, point[0].Y, point[1].X, point[1].Y);
                    graphicsPath.AddLine(point[0].X, point[0].Y, point[2].X, point[2].Y);
                    bitmapGraphics.FillPath(brush, graphicsPath);
                    graphicsPath.Dispose();
                    bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone);
                }
            }
        }
        
        Color backColor = bitmap.GetPixel(0, 0);
        bitmap.MakeTransparent(backColor);
        //---/透明圆角边框---
        
        Rectangle rectangle = new Rectangle(backgroundImage_x, backgroundImage_y, bitmap.Width, bitmap.Height); //叠图相对于底图的坐标
        g.DrawImage(bitmap, rectangle, 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel);
    }

    //圆角矩形边框叠加在背景图上 - 保持背景图和边框的合适位置
    using (Graphics g = Graphics.FromImage(backgroundImage))
    {
    
    
        //画笔
        Pen shadowPen = new Pen(textColor, 2);

        //矩形大小=
        int radius = 30;
        int rectangle_x = 40;
        int rectangle_y = 350;
        int rectangleWidth = 590;
        int rectangleHeight = 860;
        Rectangle rectangle= new Rectangle(rectangle_x, rectangle_y, rectangleWidth, rectangleHeight); //

        //线条路径
        GraphicsPath gp = new GraphicsPath();
        gp.AddArc(rectangle.X, rectangle.Y, radius, radius, 180, 90);
        gp.AddArc((rectangle.Width - 0) - radius, rectangle.Y, radius, radius, 270, 90);
        gp.AddArc((rectangle.Width - 0) - radius, (rectangle.Height - 0) - radius, radius, radius, 0, 90);
        gp.AddArc(rectangle.X, (rectangle.Height - 0) - radius, radius, radius, 90, 90);
        gp.CloseAllFigures();

        g.DrawPath(shadowPen, gp);
    }
}

6)左边小半圆圈边框
在这里插入图片描述

if (true)
{
    
    
    //先创建一个小圆圈背景
    int width = 600;
    int height = 600;

    Image smallEllipse = new Bitmap(width, height);

    using (Graphics graphics = Graphics.FromImage(smallEllipse))
    {
    
    
        //用笔刷画上蓝色背景
        Color bg = ColorTranslator.FromHtml("#0aadff");
        graphics.FillRectangle(new SolidBrush(bg), new Rectangle(0, 0, smallEllipse.Width, smallEllipse.Height));

        //绘制矩形框
        Pen pen = new Pen(textColor);
        pen.Width = 30;
        graphics.DrawEllipse(pen, 10, 10, width - 50, height - 35); //DrawRectangle 画矩形边框
    }

    //小半圆圈边框
    using (Graphics graphics = Graphics.FromImage(backgroundImage))
    {
    
    
        Rectangle _r = new Rectangle(39, 700, 40, 40); //叠图相对于底图的坐标
        
        graphics.DrawImage(smallEllipse, _r, 300, 0, smallEllipse.Width, smallEllipse.Height, GraphicsUnit.Pixel);
    }
}

7)左边左边小圆圈背景
小圆圈与背景色一样,这样就可以叠加在小圆圈边框里就可以达到图中的效果
在这里插入图片描述
8)右边小半圆圈边框
在这里插入图片描述

if (true)
{
    
    
    //先创建一个小圆圈背景
    int width = 600;
    int height = 600;

    Image smallEllipse = new Bitmap(width, height);

    using (Graphics graphics = Graphics.FromImage(smallEllipse))
    {
    
    
        //用笔刷画上蓝色背景
        Color bg = ColorTranslator.FromHtml("#0aadff");
        graphics.FillRectangle(new SolidBrush(bg), new Rectangle(0, 0, smallEllipse.Width, smallEllipse.Height));

        //绘制矩形框
        Pen pen = new Pen(textColor);
        pen.Width = 30;
        graphics.DrawEllipse(pen, 20, 20, width - 50, height - 35); //DrawRectangle 画矩形边框
    }

    using (Graphics graphics = Graphics.FromImage(backgroundImage))
    {
    
    
        Rectangle _r = new Rectangle(551, 700, 40, 40); //叠图相对于底图的坐标

        graphics.DrawImage(smallEllipse, _r, -300, 0, smallEllipse.Width, smallEllipse.Height, GraphicsUnit.Pixel);
    }
}

9)右边小圆圈背景
在这里插入图片描述

if (true)
{
    
    
    //先创建一个小圆圈背景
    int width = 600;
    int height = 600;

    Image smallEllipse = new Bitmap(width, height);
    using (Graphics graphics = Graphics.FromImage(smallEllipse))
    {
    
    
        //用笔刷画上蓝色背景
        Color bg = ColorTranslator.FromHtml(backgroundColor);
        graphics.FillRectangle(new SolidBrush(bg), new Rectangle(0, 0, smallEllipse.Width, smallEllipse.Height));
    }

    //---设置成圆形图片---
    Bitmap bitmap = new Bitmap(smallEllipse.Width, smallEllipse.Height);
    Rectangle rec = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
    Size size = new Size(bitmap.Height, bitmap.Height);

    using (Graphics g = Graphics.FromImage(bitmap))
    {
    
    
        using (TextureBrush br = new TextureBrush(smallEllipse, System.Drawing.Drawing2D.WrapMode.Clamp, rec))
        {
    
    
            br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.FillEllipse(br, new Rectangle(Point.Empty, size));
        }
    }

    using (Graphics graphics = Graphics.FromImage(backgroundImage))
    {
    
    
        Rectangle _r = new Rectangle(575, 703, 33, 33); //叠图相对于底图的坐标

        graphics.DrawImage(bitmap, _r, 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel);
    }
    //---/设置成圆形图片
}

10)边框内画虚线
在这里插入图片描述

if (true)
{
    
    
    using (Graphics graphics = Graphics.FromImage(backgroundImage))
    {
    
    
        using (Pen pen = new Pen(textColor, 2))
        {
    
    
            pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; //虚线
            graphics.DrawLine(pen, 60, 720, 572, 720); //两个坐标点组成一条线
        }
    }
}

11)描述内容
这里对于换行的显示,同样是根据标题那里的算法来,当然,也许你们有更好的方法,可以分享下哦

if (true)
{
    
    
    FontStyle fontStyle = FontStyle.Regular;
    int fontSize = 14;
    string fontFamily = "黑体";

    string text = "有时候提高自身技术以及验证基础掌握扎实程度,就可以模仿各大互联网大佬的一些功能,比如,本次模仿的一个扫一扫二维码跳转页面生成一个分享海报的功能,小5用自己擅长的C#开发语言,小小实现一下,当然,各位同行也可以使用自己的开发语言挑战下,检验下自己完成需要多少时间以及用到了那些功能和插件";
    List<string> displayText = new List<string>();
    float maxTextWidth = 630 - 270; //100长度
    string tempText = "";
    float tempWidth = 0;
    float count = 0;
    foreach (char item in text)
    {
    
    
        using (Graphics g = Graphics.FromImage(backgroundImage))
        {
    
    
            g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿

            using (Font f = new Font(fontFamily, fontSize, fontStyle))
            {
    
    
                using (Brush b = new SolidBrush(textColor))
                {
    
    
                    SizeF size = g.MeasureString(item.ToString(), new Font(fontFamily, fontSize, fontStyle));

                    if (tempWidth > maxTextWidth)
                    {
    
    
                        displayText.Add(tempText);
                        tempText = "";
                        tempWidth = 0;

                        continue;
                    }

                    tempText += item.ToString();
                    tempWidth += size.Width;

                    if (count >= 100)
                    {
    
    
                        break;
                    }
                }
            }
        }

        count++;
    }

    if (text.Length > 100)
    {
    
    
        displayText[displayText.Count - 1] += "...";
    }

    //循环显示
    int i = 0;
    foreach(string item in displayText)
    {
    
    
        using (Graphics g = Graphics.FromImage(backgroundImage))
        {
    
    
            g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
            using (Font f = new Font(fontFamily, fontSize + 10, fontStyle))
            {
    
    
                using (Brush b = new SolidBrush(textColor))
                {
    
    
                    SizeF size = g.MeasureString(item, new Font(fontFamily, fontSize + 10, fontStyle));
                    float x = 60; //(backgroundImage.Width - size.Width) / 2; //水平居中
                    float y = 380 + i * size.Height; //可以通过占比来大概计算位置=(88/545)*1120=180
                    g.DrawString(item, f, b, x, y);
                }
            }
        }

        i++;
    }
}

12)头像图片
这里需要将矩形图片设置成圆形图片
在这里插入图片描述

if (true)
{
    
    
    Image image = new Bitmap(完整本地路径头像图片路径);

    int height = 100;
    int width = height;

    //---设置成圆形图片 - 当然这里有多处用到,可以单独设置一个圆形方法出来
    Bitmap bitmap = new Bitmap(image.Width, image.Height);
    Rectangle rec = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
    Size size = new Size(bitmap.Height, bitmap.Height);

    using (Graphics g = Graphics.FromImage(bitmap))
    {
    
    
        using (TextureBrush br = new TextureBrush(image, System.Drawing.Drawing2D.WrapMode.Clamp, rec)) //关键一步
        {
    
    
            br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.FillEllipse(br, new Rectangle(Point.Empty, size));
        }
    }
    //---/设置成圆形图片

    using (Graphics graphics = Graphics.FromImage(backgroundImage))
    {
    
    
        int imageInBackgroundImage_x = 60;
        int imageInBackgroundImage_y = 740; //这里的高宽度就可以对原图进行放大缩小显示
        Rectangle _r = new Rectangle(imageInBackgroundImage_x, imageInBackgroundImage_y, width, height); //叠图相对于底图的坐标

        int imageSelf_x = 0; //相对叠图自己的坐标系x轴,假设宽度为50,那么设置x=50,那么相对于自己的体系,就超出了自己的宽度,就看不见了
        int imageSelf_y = 0; //相对叠图自己的坐标系y轴
        graphics.DrawImage(bitmap, _r, imageSelf_x, imageSelf_y, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel);
    }
}

13)昵称
在这里插入图片描述

if (true)
{
    
    
    string nickName = "Lmy_520";
    int fontSize = 15;
    string fontFamily = "黑体";
    FontStyle fontStyle = FontStyle.Regular;

    using (Graphics g = Graphics.FromImage(backgroundImage))
    {
    
    
        g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
        using (Font f = new Font(fontFamily, fontSize + 10, fontStyle))
        {
    
    
            using (Brush b = new SolidBrush(textColor))
            {
    
    
                SizeF size = g.MeasureString(nickName, new Font(fontFamily, fontSize + 10, fontStyle));
                float x = 180; //(backgroundImage.Width - size.Width) / 2; //水平居中
                float y = 770; //可以通过占比来大概计算位置=(88/545)*1120=180
                g.DrawString(nickName, f, b, x, y);
            }
        }
    }
}

14)底部黑色半透明矩形遮罩图片
在这里插入图片描述

if (true)
{
    
    
    int imageWidth = 630;
    int imageHeight = 200;
    Image image = new Bitmap(imageWidth, imageHeight);
    using (Graphics graphics = Graphics.FromImage(image))
    {
    
    
        //用笔刷画上蓝色背景
        Color bg = Color.FromArgb(50, ColorTranslator.FromHtml("#000")); //设置一定值的透明度 0~255
        graphics.FillRectangle(new SolidBrush(bg), new Rectangle(0, 0, image.Width, image.Height));
    }

    //将半透明矩形叠加在背景图上
    using (Graphics graphics = Graphics.FromImage(backgroundImage))
    {
    
    
        int imageInBackgroundImage_x = 0;
        int imageInBackgroundImage_y = 1120 - 200; //这里的高宽度就可以对原图进行放大缩小显示
        Rectangle _r = new Rectangle(imageInBackgroundImage_x, imageInBackgroundImage_y, image.Width, image.Height); //叠图相对于底图的坐标

        int imageSelf_x = 0; //相对叠图自己的坐标系x轴,假设宽度为50,那么设置x=50,那么相对于自己的体系,就超出了自己的宽度,就看不见了
        int imageSelf_y = 0; //相对叠图自己的坐标系y轴
        graphics.DrawImage(image, _r, imageSelf_x, imageSelf_y, image.Width, image.Height, GraphicsUnit.Pixel);
    }
}

15)大牛邀请你看优质博文
这里文本分成两部分,因为颜色不一样
在这里插入图片描述

if (true)
{
    
    
    string oneText = "大牛邀请你看";
    string twoText = "优质博文";
    int fontSize = 18;
    string fontFamily = "微软雅黑";
    FontStyle fontStyle = FontStyle.Bold;

    using (Graphics g = Graphics.FromImage(backgroundImage))
    {
    
    
        g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
        using (Font f = new Font(fontFamily, fontSize + 10, fontStyle))
        {
    
    
            SizeF size;
            using (Brush b = new SolidBrush(textColor))
            {
    
    
                size = g.MeasureString(oneText, new Font(fontFamily, fontSize + 10, fontStyle));
                g.DrawString(oneText, f, b, 20, 960);
            }
            using (Brush b = new SolidBrush(ColorTranslator.FromHtml("#f60")))
            {
    
    
                g.DrawString(twoText, f, b, 10 + size.Width, 960);
            }
        }
    }
}

16)阅读全文+矩形背景图片
在这里插入图片描述

if (true)
{
    
    
    //创建矩形图片
    int imageWidth = 420;
    int imageHeight = 53;
    Image image = new Bitmap(imageWidth, imageHeight);
    using (Graphics graphics = Graphics.FromImage(image))
    {
    
    
        //用笔刷画上蓝色背景
        Color bg = ColorTranslator.FromHtml("#f60"); //设置一定值的透明度 0~255
        graphics.FillRectangle(new SolidBrush(bg), new Rectangle(0, 0, image.Width, image.Height));
    }

    //阅读全文文本叠加在矩形居中位置上
    string oneText = "阅读全文";
    int fontSize = 10;
    string fontFamily = "微软雅黑";
    FontStyle fontStyle = FontStyle.Regular;

    using (Graphics g = Graphics.FromImage(image))
    {
    
    
        g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
        using (Font f = new Font(fontFamily, fontSize + 10, fontStyle))
        {
    
    
            SizeF size;
            using (Brush b = new SolidBrush(textColor))
            {
    
    
                size = g.MeasureString(oneText, new Font(fontFamily, fontSize + 10, fontStyle));
                g.DrawString(oneText, f, b, (image.Width-size.Width) / 2, (image.Height - size.Height) / 2);
            }
        }
    }

    //将矩形叠加在背景图上
    using (Graphics graphics = Graphics.FromImage(backgroundImage))
    {
    
    
        int imageInBackgroundImage_x = 0;
        int imageInBackgroundImage_y = 1120 - 80; //这里的高宽度就可以对原图进行放大缩小显示
        Rectangle _r = new Rectangle(imageInBackgroundImage_x, imageInBackgroundImage_y, image.Width, image.Height); //叠图相对于底图的坐标

        int imageSelf_x = 0; //相对叠图自己的坐标系x轴,假设宽度为50,那么设置x=50,那么相对于自己的体系,就超出了自己的宽度,就看不见了
        int imageSelf_y = 0; //相对叠图自己的坐标系y轴
        graphics.DrawImage(image, _r, imageSelf_x, imageSelf_y, image.Width, image.Height, GraphicsUnit.Pixel);
    }
}

17)二维码+跳转链接
在这里插入图片描述

if (true)
{
    
    
    string link = "https://blog.csdn.net/lmy_520/article/details/106050684";
    string filePath = QR(link, 50); //这里需要二维码生成的插件
    Image image = new Bitmap(二维码生成方法);

    //将矩形叠加在背景图上
    using (Graphics graphics = Graphics.FromImage(backgroundImage))
    {
    
    
        int imageInBackgroundImage_x = 460;
        int imageInBackgroundImage_y = 950; //这里的高宽度就可以对原图进行放大缩小显示
        Rectangle _r = new Rectangle(imageInBackgroundImage_x, imageInBackgroundImage_y, 140, 140); //叠图相对于底图的坐标

        int imageSelf_x = 0; //相对叠图自己的坐标系x轴,假设宽度为50,那么设置x=50,那么相对于自己的体系,就超出了自己的宽度,就看不见了
        int imageSelf_y = 0; //相对叠图自己的坐标系y轴
        graphics.DrawImage(image, _r, imageSelf_x, imageSelf_y, image.Width, image.Height, GraphicsUnit.Pixel);
    }
}

总结

       当然,你可以更加简单来,加载一张模板背景图,然后标题和描述以及头像和昵称是动态就可以了,这里主要是以C#多知识点全动态化来实现下,也许,聪明的你肯定有比小5这个更厉害的实现方式方法。
       代码写的比较罗嗦,其实有些代码重复的比较多,可以整理出一些公共方法,这里为了展示每一步就懒得写了,哈哈哈!需要单独源码的,我可以整理下代码发给你们哈。

互动一下

复制你文章的链接,公众号【互联网功能小例子】回复的文章链接,就可以生成一张同款海报二维码

猜你喜欢

转载自blog.csdn.net/lmy_520/article/details/106050684
今日推荐