当前位置: 代码迷 >> 综合 >> 在WEB程序中如何画图并显示
  详细解决方案

在WEB程序中如何画图并显示

热度:13   发布时间:2024-01-22 00:15:25.0
大家都知道,在窗体上画图形,并显示出来很容易,只要得到相关容器的Graphics,就可以按照自己想要的样式去画;但是在WEB中,想要在某个页面中去随意画出图形并且显示出来则不能像窗体程序那样,因为不能获得某个容器的Graphics,这是B/S的架构有关,毕竟显示的页面是在Client,而你的处理地方是在Server端。
 
那么如何在WEB程序中如何画图并显示,则需要一定设置,大致步骤如下:
 
首先,需要设置WEB程序运行的目录下,设置一个临时目录,用于存放临时的图片文件,例如:“ImagesTemp”,并设置ASPNET用户能对此目录可写。
 
接下来,画图的思路,是动态生成一个Bitmap,用它产生容器,从而得到Graphics;然后用此Graphics来画图;完毕后,把Bitmap中的内容保存到上面设置的临时目录中;再把页面中某个显示控件的ImageUrl指定到刚生成的图形临时文件,即可显示。大致代码如下:
        // Create panel
        Bitmap bitImage = new Bitmap( 200, 200 );
 
        // Create graphics
        Graphics newGraphics = Graphics.FromImage((System.Drawing.Image)bitImage);
 
        // Draw image
        newGraphics.FillRectangle(new SolidBrush(Color.Coral), 20, 20, 120, 80);
 
        // Save image
        string strTempFileName = Server.MapPath( "Images" ) + "/ImgTemp.jpg";
        bitImage.Save( strTempFileName );
 
        // Draw image to screen.
        imgTest.ImageUrl = @"Images/ImgTemp.jpg";
        // Release graphics object.
        newGraphics.Dispose();
        bitImage.Dispose();
 
  相关解决方案