现在的位置: 首页 > 综合 > 正文

datagridview单元格重绘

2012年08月25日 ⁄ 综合 ⁄ 共 3520字 ⁄ 字号 评论关闭

哈希表存放要绘制的图片

 private int _width;
        private int _height;
        private string _fontFamily;
        private int _fontSize;
        private bool _adaptable;
        private FontStyle _fontStyle;
        private bool _shadow;
        private string _backgroundImage;
        private Color _bgColor;
        private int _left;
        private string _resultImage;
        private string _text;
        private int _top;
        private int _alpha;
        private int _red;
        private int _green;
        private int _blue;
        private long _quality;
        workflow.Watermark watermark = new Watermark();
        Hashtable ht1 = new Hashtable();//通过哈希表来存储图片
  private void dataGridViewX1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
 if (e.ColumnIndex == 0 && e.Value != null && e.RowIndex != -1 && e.ColumnIndex != -1)
            {
                _fontFamily = "华文行楷";
                _fontSize = 20;
                _fontStyle = FontStyle.Regular;
                _adaptable = true;
                _shadow = false;
                _left = 0;
                _top = 0;
                _alpha = 255;
                _red = 0;
                _green = 0;
                _blue = 0;
                _backgroundImage = "";
                _quality = 100;
                _text = e.Value.ToString();
                _bgColor = Color.FromArgb(255, 229, 229, 229);
                Graphics g;
                StringFormat StrFormat = new StringFormat();
                Brush b2 = new SolidBrush(Color.FromArgb(90, 0, 0, 0));
                Brush b = new SolidBrush(Color.FromArgb(_alpha, _red, _green, _blue));
                Font f = new Font(_fontFamily, _fontSize, _fontStyle);
                Bitmap bitmap;
                for (int i = 0; i < dataGridViewX1.Rows[0].Cells.Count; i++)
                {
                    _width += this.dataGridViewX1.Rows[e.RowIndex].Cells[i].Size.Width;//每一行单元格宽度
                    _height = this.dataGridViewX1.Rows[e.RowIndex].Cells[i].Size.Height;
                }
                if (this._backgroundImage.Trim() == "")
                {
                    bitmap = new Bitmap(this._width, this._height, PixelFormat.Format64bppArgb);
                    g = Graphics.FromImage(bitmap);
                    g.Clear(this._bgColor);
                }
                else
                {
                    bitmap = new Bitmap(Image.FromFile(this._backgroundImage));
                    g = Graphics.FromImage(bitmap);
                }
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.CompositingQuality = CompositingQuality.HighQuality;
                g = Graphics.FromImage(bitmap);
                g.DrawString(_text, f, b2, _left + 2, _top + 1);
                //    }
                g.DrawString(_text, f, b, new PointF(_left, _top), StrFormat);
                SizeF size = g.MeasureString(_text, f);
                while (_adaptable == true && size.Width > bitmap.Width)
                {
                    _fontSize--;
                    f = new Font(_fontFamily, _fontSize, _fontStyle);
                    size = g.MeasureString(_text, f);
                }
                StrFormat.Alignment = StringAlignment.Near;
                // bitmap.Save(strtest, ImageFormat.Gif);//将添加水印的图片输入到当前流中
                if (ht1[e.RowIndex + 1] == null)
                {
                    ht1.Add(e.RowIndex + 1, bitmap);
                }
            }
            if (ht1[e.RowIndex + 1] != null && e.ColumnIndex != -1 && e.RowIndex != -1)
            {
                Bitmap bitmap = new Bitmap((Image)ht1[e.RowIndex + 1]);
                Graphics g = Graphics.FromImage(bitmap);
                int width = 0;
                int heigh = this.dataGridViewX1.Rows[e.RowIndex].Cells[e.ColumnIndex].Size.Height;
                int portionHeight = this.dataGridViewX1.Rows[e.RowIndex].Cells[e.ColumnIndex].Size.Height;
                int portionWidth = this.dataGridViewX1.Rows[e.RowIndex].Cells[e.ColumnIndex].Size.Width;
                for (int d2 = 0; d2 < e.ColumnIndex; d2++)
                {
                    int width1 = this.dataGridViewX1.Rows[e.RowIndex].Cells[d2].Size.Width;
                    width += width1;
                }
                e.Graphics.DrawImage((Image)ht1[e.RowIndex + 1], e.CellBounds.X, e.CellBounds.Y, new Rectangle(width, 0, portionWidth, portionHeight), GraphicsUnit.Pixel);
                e.Handled = true;
            }

 

抱歉!评论已关闭.