zoukankan      html  css  js  c++  java
  • WinFrom程序如何从数据库循环读取图片并合成

    数据库中有表backgroundimage(背景图片表),backgroundpoint(图片坐标表),systemheadimage(头像表),分别存放的背景,坐标及头像,我现在要对应遍历出三表相关信息并合成一张图片,合成方法我已经做好,如下代码:

    C# code 

    原文转自:http://club.itqun.net/showtopic-257319.html

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    namespace BaiShun
    {
       
    publicclass Helper
        {
           
    #region Helper
           
    ///<summary>
           
    /// 原图的大小
           
    ///</summary>
            publicint calcWidth(int h, Image image)
            {
               
    int w =0;
               
    //计算比例
               
    //w/h = nw/x   x*w = h*nw; x = h*nw/w
                w =Convert.ToInt32( image.Width * h / image.Height);
               
    return w;
            }
           
    ///<summary>
           
    /// 计算两点间长度
           
    ///</summary>
            publicint getLength(int x1, int y1, int x2, int y2)
            {
               
    double distance = Math.Pow(Math.Pow((x1 - x2), 2) + Math.Pow((y1 - y2), 2), 0.5);
               
    return Convert.ToInt32( distance);
            }
           
    ///<summary>
           
    /// 计算角度
           
    ///</summary>
           
    ///<param name="px1">p1 X轴</param>
           
    ///<param name="py1">p1 y轴</param>
           
    ///<param name="px2">p2 X轴</param>
           
    ///<param name="py2">p2 y轴</param>
           
    ///<returns></returns>
            publicdouble getAngle(double px1, double py1, double px2, double py2)
            {
               
    double x = px2 - px1;
               
    double y = py2 - py1;
               
    double hyp = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
               
    double cos = x / hyp;
               
    double rad = Math.Acos(cos);
               
    double deg =180/ (Math.PI / rad);
               
    if (y <0) { deg =-deg; }
               
    elseif ((y ==0) && (x <0)) { deg =180; }

               
    return deg;
            }
           
    ///<summary>
           
    /// 计算角度
           
    ///</summary>
            public Image RotateImg(Image b, int angle)
            {
                angle
    = angle %360;
               
    //弧度转换
                double radian = angle * Math.PI /180.0;
               
    double cos = Math.Cos(radian);
               
    double sin = Math.Sin(radian);
               
    //原图的宽和高
                int w = b.Width;
               
    int h = b.Height;
               
    int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
               
    int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
               
    //目标位图
                Bitmap dsImage =new Bitmap(W, H);
                Graphics g
    = Graphics.FromImage(dsImage);
               
    /*坐标结束*/
                g.InterpolationMode
    = InterpolationMode.Default;//Bilinear;
                g.SmoothingMode = SmoothingMode.Default;// HighQuality;
               
    //计算偏移量
                Point Offset =new Point((W - w) /2, (H - h) /2);
               
    //构造图像显示区域:让图像的中心与窗口的中心点一致
                Rectangle rect =new Rectangle(Offset.X, Offset.Y, w, h);
                Point center
    =new Point(rect.X + rect.Width /2, rect.Y + rect.Height /2);
                g.TranslateTransform(center.X, center.Y);
                g.RotateTransform(
    360- angle);
               
    //恢复图像在水平和垂直方向的平移
                g.TranslateTransform(-center.X, -center.Y);
                g.DrawImage(b, rect);
               
    //重至绘图的所有变换
                g.ResetTransform();
                g.Save();
                g.Dispose();
                b.Dispose();
               
    return dsImage;
            }
           
    ///<summary>
           
    /// 厘米转像素 根据drawGraphicsDPI—X轴
           
    ///</summary>
            publicint cm_To_px_ByX(int length)
            {
               
    return Convert.ToInt32(length *72/2.54);
            }
           
    ///<summary>
           
    /// 厘米转像素 根据drawGraphicsDPI—Y轴
           
    ///</summary>
            publicint cm_To_px_ByY(int width)
            {
               
    return Convert.ToInt32(width *72/2.54);
            }
           
    ///<summary>
           
    /// 像素转厘米 根据drawGraphicsDPI—X轴
           
    ///</summary>
            publicfloat px_To_cm_ByX(int length)
            {
               
    return Convert.ToSingle(length *2.54/72);
            }
           
    ///<summary>
           
    /// 像素转厘米 根据drawGraphicsDPI—Y轴
           
    ///</summary>
            publicint px_To_cm_ByY(int width)
            {
               
    return Convert.ToInt32(width *2.54/72);
            }
           
    #endregion Helper
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Drawing;
    namespace BaiShun
    {
       
    publicclass SyntheticHelper
        {
           
    privatestatic Helper helper =new Helper();
           
    ///<summary>
           
    /// 设置图片
           
    ///</summary>
            publicstatic Image settingPic(Image originalBackgroundImage, Image originalHeadImage, int p1_X, int p1_Y, int p2_X, int p2_Y)
            {
               
    ///************************绘制背景************************/
                //1.    新建backgroundImage。
                Image backgroundImage =new Bitmap(originalBackgroundImage.Width, originalBackgroundImage.Height);
               
    //2.    drawBackgroundImagePoint定义backgroundImage背景坐标系
                Point[] drawBackgroundImagePoint =new Point[] { new Point(0, 0), new Point(originalBackgroundImage.Width, 0), new Point(0, originalBackgroundImage.Height) };
               
    //      获得图面
                Graphics drawGraphicsBackground = Graphics.FromImage(backgroundImage);
               
    //3.    将originalBackgroundImage原始背景图绘制上去(backgroundImage)
                drawGraphicsBackground.DrawImage(originalBackgroundImage, drawBackgroundImagePoint);
                drawGraphicsBackground.Dispose();
               
    //backgroundImage
               
    //***********************头像***********************
                int tempY = p2_Y - p1_Y;
               
    int tempX = p2_X - p1_X;
               
    double radian = Math.Atan2((double)(tempY), (double)(tempX));
               
    int angle1 = Convert.ToInt32(radian * (180/ Math.PI));
                radian
    = angle1 * (Math.PI /180);
               
    double t1 = Math.Cos(radian);
               
    double t2 = Math.Sin(radian);
               
    double t3 = Math.Cos(-radian);
               
    double t4 = Math.Sin(-radian);
               
    /*追加的素材图片*/
               
    //      *1.    根据p1,p2点计算两点间的距离。可以得出缩放图片的高度h
                int h = helper.getLength(p1_X, p1_Y, p2_X, p2_Y);
               
    //      *2.    根据h计算出w
                int w = helper.calcWidth(h, originalHeadImage);
               
    //1.    计算缩放长宽,使用originalHeadImage创建zoomImage图片。(图片已被缩放)
                Bitmap zoomImage =new Bitmap(originalHeadImage, w, h);
               
    //2.    根据坐标p1,p2点计算角度angle(公式A)
                double angle =Math.Abs( helper.getAngle(p1_X, p1_Y, p2_X, p2_Y));
              
    // double radian = angle * (Math.PI / 180);
               
    //3.    根据zoomImage计算旋转图形生成rotateImage
                Image rotateImage = helper.RotateImg(zoomImage, Convert.ToInt32(-90+ Math.Abs(angle)));
               
    //4.    将angle绝对值修正成正坐标。
               
    //angle = angle);
               
    //5.    取得backgroundImage背景绘画方法。准备绘图
                Graphics drawGraphics = Graphics.FromImage(backgroundImage);
               
    //6.    计算修正坐标[Revised_X]和[Revised_Y]。
                int Revised_X = Convert.ToInt32(Math.Cos(90- angle) * (w /2));
               
    int Revised_Y = Convert.ToInt32(Math.Sin(90- angle) * (w /2));
               
    //7.    声明p3点
                int p3_X =0;
               
    int p3_Y =0;

               
    //8.    判断
                if (p1_X == p2_X || p1_Y == p2_Y)
                {
                   
    //如果是垂直形[坐标位置直接减去rotateImage宽度的一半]。
                    p3_X = Convert.ToInt32(p2_X - Math.Abs(Revised_X));
                    p3_Y
    = Convert.ToInt32(p2_Y + Math.Abs(Revised_Y));
                   
    //垂直型
                   
    //  9.绘制图形输出
                    drawGraphics.DrawImage(rotateImage, new Point(p3_X, p3_Y));
                }
               
    else
                {
                  
    // drawGraphics.TranslateTransform(0, 140);// 平移到5,5坐标

                   
    double temp = Math.Sin(radian);
                    temp
    /=2;
                   
    double temp2 = Math.Cos(radian);
                    p3_X
    = Convert.ToInt32((float)p1_X + (float)w * temp );
                    p3_Y
    = Convert.ToInt32((float)p1_Y + (float)h * Math.Sin(radian) - w * temp2 /2);
                   
    //其它型
                   
    //  9.绘制图形输出
                    drawGraphics.DrawImage(rotateImage, new Point(p3_X, p3_Y));
                }
                drawGraphics.Dispose();
               
    return backgroundImage;
            }

           
    ///<summary>
           
    /// 设置文字
           
    ///</summary>
            publicstatic Image settingFont(Image image, String Text, int x, int y)
            {
               
    //获得画笔
                Graphics drawGraphicsFont = Graphics.FromImage(image);
                Brush brush
    =new System.Drawing.SolidBrush(System.Drawing.Color.Black);

               
    //绘制文字
                drawGraphicsFont.DrawString(Text, new Font("宋体", 15), brush, x, y);
                drawGraphicsFont.Dispose();
               
    return image;
            }


        }
    }

  • 相关阅读:
    CF1042E Vasya and Magic Matrix
    Luogu 4868 Preprefix sum
    CF1042F Leaf Sets
    CF1041F Ray in the tube
    【Luogu】P1410子序列(DP)
    【Luogu】P1383高级打字机
    【Luogu】P1681最大正方形2(异或运算,DP)
    【Luogu】P1122最大子树和(DFS,树上DP)
    【Luogu】P2258子矩阵(状态压缩,DP)
    【Luogu】P2158仪仗队(欧拉函数)
  • 原文地址:https://www.cnblogs.com/wuhuisheng/p/2471852.html
Copyright © 2011-2022 走看看