随机图片生成器制作方法分享(3)

// 将位图背景填充为白色
        System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);
        graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);
        graph.Dispose();

double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;

for (int i = 0; i < destBmp.Width; i++)
        {
            for (int j = 0; j < destBmp.Height; j++)
            {
                double dx = 0;
                dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
                dx += dPhase;
                double dy = Math.Sin(dx);

// 取得当前点的颜色
                int nOldX = 0, nOldY = 0;
                nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
                nOldY = bXDir ? j : j + (int)(dy * dMultValue);

System.Drawing.Color color = srcBmp.GetPixel(i, j);
                if (nOldX >= 0 && nOldX < destBmp.Width
                 && nOldY >= 0 && nOldY < destBmp.Height)
                {
                    destBmp.SetPixel(nOldX, nOldY, color);
                }
            }
        }

return destBmp;
    }

#endregion

/// <summary>
    /// 生成校验码图片
    /// </summary>
    /// <param>验证码</param>
    /// <returns></returns>
    public Bitmap CreateImage(string code)
    {
        int fSize = FontSize;
        int fWidth = fSize + Padding;

int imageWidth = (int)(code.Length * fWidth) + 4 + Padding * 2;
        int imageHeight = fSize * 2 + Padding * 2;

System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageWidth - 10, imageHeight - 10);

Graphics g = Graphics.FromImage(image);

g.Clear(BackgroundColor);

//给背景添加随机生成的燥点
        if (this.Chaos)
        {

Pen pen = new Pen(ChaosColor, 0);
            int c = ChaosWight * 10;

for (int i = 0; i < c; i++)
            {
                int x = rnd.Next(image.Width);
                int y = rnd.Next(image.Height);

g.DrawRectangle(pen, x, y, 1, 1);
            }
        }

int left = 0, top = 0, top1 = 1, top2 = 1;

int n1 = (imageHeight - FontSize - Padding * 2);
        int n2 = n1 / 4;
        top1 = n2;
        top2 = n2 * 2;

Font f;
        Brush b;

int cindex, findex;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjgywf.html