美文网首页
C#创建图片保存为文件和string的例子

C#创建图片保存为文件和string的例子

作者: 价值投机168 | 来源:发表于2020-11-26 17:23 被阅读0次

    pictureBox1就是PictureBox类型的,直接拖入这个控件就行。

    创建一个from的工程,直接考入代码就行:

     public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void showPic(string pic) {
            byte[] arr = Convert.FromBase64String(pic);
            MemoryStream ms = new MemoryStream(arr);
            System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
    
            this.pictureBox1.Image = img;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            showPic("iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAEgklEQVRIS7WXbYhUZRSAn3Pv/rClH0kUaSDOzp0pPyikXbMPiVQ0V1BYfxilOYuzSlL0gUUEpZmYZZFk1IabLllqQkrB7ihSCH1g+SvR2pl7Z1wVLCpBKgp23XvijHPtOjvrB+6+vy7ve+55zjnvOe97XuEKRz6fn+667lhVHQtMFpE71xU23AUcUeWYI5xW4bvOuR17rkSlXE4on8/PdF33YWCKyYrIaeCkqp5YV3j9VtAGhAZUGkCvAw4DOwbcuh3b57T/NpT+IcG+709xHOdhVZ2pqodUdfe5c+f8iRMn/lJLWWt3602o06LitIDOBoqOyHNb527ZW0u+JrhUKi0Lw/BxETniOM7uRCKx73KRia9ncm1NqvqeQKPAym3NHe9X/z8IHASBhXWVqn6YSqUG/XA1BizNZQ+IMgvllc55HWvi/14E9n1/gYi8BOz0PO+tq4EMJZvJZVejrEGZ3Tmv40AkdwHs+/5sEVkPfO553qvDAY10ZLra2hFdITowbdu8bd+XkzRa9H1/q4j86Xne08MJ/R+ezQHjB+rqHrBsL4ODIHgQ2BiG4ep0Ot01IuBcWxOqPwDPdDZ3bIrAG4Hk2bNnFzU2NvaPBNh0Zrrb9oOO7mzumCqlUumOMAwtzB3JZLI9Br0R+ASYU5m7H/i28v0oMN2sB+orcrZk82eABUAPkI870dq1bIWKtCMslCAInjBjVHVRKpUqxgRvA5qBt6siEBnUWwG3APZtYzxgNW9zW6ojZ4eM4p4E2sX3/fWO40xOJpPzqwQNfLtledW8eXUzcD3wZgUSB/9dy9sLSZbL/oxKj3m8DejzPG9FFeA+4JvK3AcV78YBy4B3gNYKOB7qbCVK24F/a+VKJpfNodxiHu+3vUulUmsvkVTmpY0G4KuKRysr4DjAjLUxAzB9i2P7X15Y2t22WdDF5rElzC7P8zZfAmwK08DdQDwyLwPRYWM30xLASsbK8yNgkHGZ7rZNoI8ZeJddc57nPV9jL23vzDDzOPo2MUuwaqWRt39Uwj0EOPsFwlgDvwGM8zzPLof4iJdT3LNa4MjbaG/tvLdQx0uwrDuTyx5F+cn2+FkRWdjf3z9/woQJVoMjNs6XU90JVdqlWCzOUtUNYRi+m06nO0eMamUQP0AMVCwWd4dh6PT19T0yadKkvpGCX3RkGiQIguXA8jAMX0un05+NBNi6kkGXRKFQaLAWR0TO1NfXLx4zZszvww1f2p09LDB6wK2798K1aBDf95eIyFMiciiZTNr5PWwjaoEckZao+buo9QmCwEqg2XXdVYlE4uBwkDNd2TUIq6ubvlrNnjV4TdYYeJ736bXAo5bnss1eBCkUCi2O47wIfAls9zzv6NUYUE6kUNciPFTd5EV6hmzoS6XS1DAMX7BTDTDwQdd19yUSiV9rGTGooVd6hIFM1NxV/3PJJ8zx48dvCMPQbpoZqjqt958Tpz4+tfMeRH4Ezas9aES9cgOg5SZg1DU/YaotLBaLTXtO733y2F89o86/lcpXpBneC9IL2ovw9ZU+2v4DnerYhytiiykAAAAASUVORK5CYII=");
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            var imagedata = new Bitmap(150, 20);
            var sourcegra = Graphics.FromImage(imagedata);//存入画布
            sourcegra.Clear(Color.White);
           // sourcegra.DrawString("test", new Font("宋体", 20, FontStyle.Regular), new SolidBrush(Color.Black), 0, 0);//在画布绘制字体
    
            sourcegra.FillRectangle(new SolidBrush(Color.FromArgb(96, 182, 100)), new Rectangle(10, 1, 100, 5));
            sourcegra.FillRectangle( new SolidBrush(Color.FromArgb(235, 74, 70)), new Rectangle(21, 1, 10, 5));
    
            sourcegra.FillRectangle(new SolidBrush(Color.FromArgb(102, 153, 255)), new Rectangle(61, 1, 50, 5));
            sourcegra.FillRectangle(new SolidBrush(Color.FromArgb(168, 0, 168)), new Rectangle(31, 1, 30, 5));
    
           // FillRoundRect(sourcegra, new Pen(Color.Red), new Rectangle(10, 30, 100, 10), 5);
            imagedata.Save("D://1.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);//保存图片到D盘
    
            MemoryStream ms = new MemoryStream();
            imagedata.Save(ms,ImageFormat.Jpeg);
            string mypic = Convert.ToBase64String(ms.ToArray());
    
            showPic(mypic);
        }
    }

    相关文章

      网友评论

          本文标题:C#创建图片保存为文件和string的例子

          本文链接:https://www.haomeiwen.com/subject/xblziktx.html