在图片上动态的写文字

In my example, I have a picture of my son and I write the words “That’s my boy!”. Also, to show that you can combine graphic drawing and text together, I then draw an oval shape around the word that I just put on the picture.

In order to make this sample work, you need the following references:

using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing.Text;

Also, you might need to play around with where you want your text and oval to go. Remember that both the placement of the text and graphic oval are based on X and Y coordinates. So, if you have to move the text/oval up, then change the value of the Y parameter. If you need to move the text/oval horizontally, then change the value of the X parameter.

There are basically 6 steps for my sample:

  1. Load your image:
    //Load the Image to be written on.
    Bitmap bitMapImage = new 
       System.Drawing.Bitmap(Server.MapPath("dallen.jpg" )  );
    Graphics graphicImage = Graphics.FromImage( bitMapImage ); 
  2. Set the graphics to be smooth.
    //Smooth graphics is nice.
    graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
  3. Write your text. Here is where you set your font. The X and Y coordinates are in the new point. (100 = X, 250 = Y).
    //Write your text.
    graphicImage.DrawString( "That's my boy!", 
       new Font("Arial", 12,FontStyle.Bold ), 
       SystemBrushes.WindowText, new Point( 100, 250 ) ); 
  4. Draw your oval around the text. Note: play around with the numbers to make the oval to the correct size that you want.
    //I am drawing a oval around my text.
    graphicImage.DrawArc(new Pen(Color.Red, 3), 90, 235, 150, 50, 0, 360); 
  5. Set the Content Type to jpg and then write your image to the response stream.
    //Set the content type
    Response.ContentType="image/jpeg"; 
    //Save the new image to the response output stream.
    bitMapImage.Save(Response.OutputStream, ImageFormat.Jpeg); 
  6. Clean up.
    //Clean house.
    graphicImage.Dispose();
    bitMapImage.Dispose();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值