C# GDI

绘制实心矩形

using (Graphics gp = Graphics.FromImage(bmBlank))
{
//...
   int y = (int)currentPoint.Y - 5;
   Rectangle rec = new Rectangle(87, y, 40, 30);//画一个白块,即起到擦拭的效果
   gp.FillRectangle(Brushes.White, rec);

 Brushes颜色参考

绘图基本方法

string backImage = GetMaterialPath(param, this.reportType) + "image_blank_1.png";
using (Image im = Image.FromFile(backImage))
{
    using (Bitmap bmBlank = new Bitmap(230, 60, Graphics.FromImage(im)))
    {
        using (Graphics gp = Graphics.FromImage(bmBlank))
        {
            gp.DrawImage(im, new Point(0, 0));
            int width = (int)GetWidth_1(teamScore);
            Rectangle rec = new Rectangle((int)baseX, (int)baseY, width, 29);
            gp.FillRectangle(Brushes.Gold, rec);
        }
        ImageOperation.SaveImage(bmBlank, saveImgFilePath, 100000);
    }
}
//自定义工具类ImageOperation
public static void SaveImage(Bitmap myBitmap,string fileName, int quality)
{

    System.Drawing.Imaging.EncoderParameters myEncoderParameters =
        new System.Drawing.Imaging.EncoderParameters(1);

    System.Drawing.Imaging.EncoderParameter myEncoderParameter =
        new System.Drawing.Imaging.EncoderParameter(
        System.Drawing.Imaging.Encoder.Quality, quality);

    myEncoderParameters.Param[0] = myEncoderParameter;

    System.Drawing.Imaging.ImageCodecInfo myImageCodecInfo;
    myImageCodecInfo = GetEncoderInfo("image/jpeg");


    string ext = myImageCodecInfo.FilenameExtension.Split(';')[0];
    ext = System.IO.Path.GetExtension(ext).ToLower();

    string saveName = System.IO.Path.ChangeExtension(fileName, ext);

    //保存
    myBitmap.Save(saveName, myImageCodecInfo, myEncoderParameters);
}
using (FileStream fs = new FileStream(saveImgFilePath, FileMode.Create))
{
    ImageCodecInfo imageCodeInfo = ImageOperation.GetEncoderInfo("image/jpeg");
    System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
    EncoderParameters myEncoderParameters = new EncoderParameters(1);
    myEncoderParameters.Param[0] = new EncoderParameter(myEncoder, 99L);
    bmBlank.Save(fs, imageCodeInfo, myEncoderParameters);
}

原生自定义背景色

Bitmap bmBlank = new Bitmap(width, height);
using (Graphics gp = Graphics.FromImage(bmBlank))
{
    //清空画布并以白色背景色填充 
    gp.Clear(Color.White);
    for (int i = 0; i < imagePaths.Length; i++)
    {
         //加载一个图片
        string imagePath = imagePaths[i];
        //封装成Bitmap
        Bitmap bm = new Bitmap(Image.FromFile(imagePath));
        //切割此Bitmap为了等比例缩放
        Bitmap newbm = ImageOperation.Cut(bm, 200, 0, 800, 800);
        //查找所绘制每张图的新坐标
        int x = xValue[i];
        int y = yValue[i];
        gp.DrawImage(newbm, x, y, 400, 400);
        bm.Dispose();
    }
    gp.Dispose();
    //保存为图片...  
}    

 

posted @ 2017-01-18 13:40  轴轴  阅读(221)  评论(0编辑  收藏  举报