You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

383 lines
15 KiB
C#

2 months ago
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace NetLibrary.Drawing2D
{
#region 我的图片
public class MyBitmap
{
private Bitmap BackgroundImage = null;
private Graphics MyGraphics = null;
private string FilePath = "";
public MyBitmap(string FilePath)
{
BackgroundImage=CustomIO.GetBitmap(FilePath);
MyGraphics = CreateGraphics();
}
public MyBitmap(byte[] images)
{
BackgroundImage = CustomIO.GetPhoto(images);
MyGraphics = CreateGraphics();
}
#region 创建画布
private Graphics CreateGraphics()
{
Graphics gp = Graphics.FromImage(BackgroundImage);
gp.PageUnit = GraphicsUnit.Pixel;
gp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gp.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
gp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gp.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
gp.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
gp.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
return gp;
}
#endregion
#region 去除内存
public void Dispose()
{
MyGraphics.Dispose();
BackgroundImage = null;
MyGraphics = null;
}
#endregion
#region 画表格
public void DrawTable(MyTable tb)
{
FormatGroups(tb);
int Left = tb.Left;
int Top = tb.Top;
Color TextColor = Color.FromName(tb.FontColor);
string BackColor = "";
foreach (var row in tb.Rows)
{
BackColor = row.BackColor;
for (int i = 0; i < tb.Columns.Count; i++)
{
MyCell cell = row.Cells[i];
if (cell.Visible == true)
{
if (string.IsNullOrEmpty(cell.FontColor) == false) { TextColor = Color.FromName(cell.FontColor); }
else { TextColor = Color.FromName(tb.FontColor); }
if (string.IsNullOrEmpty(cell.BackColor) == false) { BackColor = cell.BackColor; }
else { BackColor = row.BackColor; }
Rectangle rect = new Rectangle(Left, Top, tb.Columns[i].Width * cell.ColumnSpan, row.Height * cell.RowSpan);
if (string.IsNullOrEmpty(BackColor) == false)
{
SolidBrush br = new SolidBrush(Color.FromName(BackColor));
MyGraphics.FillRectangle(br, rect);
br.Dispose();
}
if (tb.ShowLine == true) MyGraphics.DrawRectangle(new Pen(Color.Black, tb.BorderWidth), rect);
if (cell.Img == null)
{
TextFormatFlags flags = TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak | TextFormatFlags.NoPadding;
if (cell.TextAlign == "Center") flags = flags | TextFormatFlags.HorizontalCenter;
if (cell.TextAlign == "Right") flags = flags | TextFormatFlags.Right;
if (cell.VerticalAlign == "Center") flags = flags | TextFormatFlags.VerticalCenter;
if (cell.VerticalAlign == "Bottom") flags = flags | TextFormatFlags.Bottom;
TextRenderer.DrawText(MyGraphics, cell.Text, cell.font, rect, TextColor, flags);
}
else
{
Bitmap bmp = CustomIO.GetBitmap(cell.Img.FilePath);
int TextTop = 0;
int TextLeft = 0;
int ImgTop = 0;
int ImgLeft = 0;
SizeF fs = GetFontSize(cell.font, cell.Text);
int TextHeight = Convert.ToInt32(fs.Height);
int TextWidth = Convert.ToInt32(fs.Width);
if (cell.TextAlign == "Center")
{
TextLeft = Left + ((tb.Columns[i].Width * cell.ColumnSpan - TextWidth - cell.Img.Width) / 2);
}
if (cell.TextAlign == "Right")
{
TextLeft = Left + (tb.Columns[i].Width * cell.ColumnSpan - TextWidth - cell.Img.Width);
}
if (string.IsNullOrEmpty(cell.TextAlign) == true || cell.TextAlign == "Left")
{
TextLeft = Left;
}
if (cell.VerticalAlign == "Center")
{
TextTop = Top + ((row.Height * cell.RowSpan - TextHeight) / 2);
ImgTop = Top + ((row.Height * cell.RowSpan - cell.Img.Height) / 2);
}
if (cell.VerticalAlign == "Bottom")
{
TextTop = Top + (row.Height * cell.RowSpan - TextHeight);
ImgTop = Top + (row.Height * cell.RowSpan - cell.Img.Height);
}
if (string.IsNullOrEmpty(cell.VerticalAlign) == true || cell.VerticalAlign == "Top")
{
TextTop = Top;
ImgTop = Top;
}
ImgLeft = TextLeft + TextWidth;
MyGraphics.DrawString(cell.Text, cell.font, new SolidBrush(TextColor), TextLeft, TextTop);
MyGraphics.DrawImage(bmp, ImgLeft, ImgTop, cell.Img.Width, cell.Img.Height);
}
}
Left += tb.Columns[i].Width;
}
Left = tb.Left;
Top += row.Height;
}
}
#endregion
#region 画文字
public void DrawString(MyString item)
{
FontStyle style = FontStyle.Regular;
if (item.IsBold == true) style = style | FontStyle.Bold;
if (item.IsItalic == true) style = style | FontStyle.Italic;
Font f = new Font(item.FontName, item.FontSize, style);
TextFormatFlags flags = TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak | TextFormatFlags.NoPadding;
if (item.TextAlign == "Center") flags = flags | TextFormatFlags.HorizontalCenter;
if (item.TextAlign == "Right") flags = flags | TextFormatFlags.Right;
if (item.VerticalAlign == "Center") flags = flags | TextFormatFlags.VerticalCenter;
if (item.VerticalAlign == "Bottom") flags = flags | TextFormatFlags.Bottom;
Color TextColor = Color.FromName(item.FontColor);
Rectangle rect = new Rectangle(item.Left, item.Top, item.Width, item.Height);
TextRenderer.DrawText(MyGraphics, item.Text, f, rect, TextColor, flags);
}
#endregion
#region 画图片
public void DrawImage(MyImage item)
{
Bitmap bmp = CustomIO.GetBitmap(item.FilePath);
//第一个柜形为要画的图片大小和坐标,第二个为要截取的图片坐标和大小
MyGraphics.DrawImage(bmp, new Rectangle(0, 0, item.Width, item.Height), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
}
#endregion
#region 保存图片
public void Save(ImageFormat imgFormat)
{
if (string.IsNullOrEmpty(this.FilePath) == false) throw new Exception("保存文件路径不存在");
BackgroundImage.Save(this.FilePath,imgFormat);
}
#endregion
#region 另存为图片
public void SaveAs(string filePath, ImageFormat imgFormat)
{
if (File.Exists(filePath) == true) File.Delete(filePath);
BackgroundImage.Save(filePath, imgFormat);
}
#endregion
#region 返回行高
public int GetFontHeight(Font f)
{
return TextRenderer.MeasureText("测试", f).Height;
}
#endregion
#region 返回字体大小
public SizeF GetFontSize(Font f,string Text)
{
// return TextRenderer.MeasureText(Text, f);
return MyGraphics.MeasureString(Text, f);
}
#endregion
#region 合并单元格
void FormatGroups(MyTable tb)
{
FontStyle style = FontStyle.Regular;
if (tb.IsBold == true) style = style | FontStyle.Bold;
if (tb.IsItalic == true) style = style | FontStyle.Italic;
Font CurrentFont = new Font(tb.FontName, tb.FontSize * 72 / BackgroundImage.HorizontalResolution, style);
foreach (MyRow row in tb.Rows)
{
int rowIndex = tb.Rows.IndexOf(row);
for (int i = 0; i < tb.Columns.Count; i++)
{
MyCell cell = row.Cells[i];
if (string.IsNullOrEmpty(cell.FontName) == true)
{
cell.font = CurrentFont;
}
else
{
style = FontStyle.Regular;
if (cell.IsBold == true) style = style | FontStyle.Bold;
if (cell.IsItalic == true) style = style | FontStyle.Italic;
cell.font = new Font(cell.FontName, cell.FontSize * 72 / BackgroundImage.HorizontalResolution, style);
}
if (cell.RowSpan > 1)
{
//如果跨行数等于>1,那么添加,计算本单元格所需高度
//cell.Height = cell.Height * cell.RowSpan;
for (int ii = 1; ii < cell.RowSpan; ii++)
{
tb.Rows[rowIndex + ii].Cells[i].Visible = false;
tb.Rows[rowIndex + ii].Cells[i].VisibleState = false;
}
}
if (cell.ColumnSpan > 1)
{
//如果跨列数大于1那么计算本单元格所需宽度
//cell.Width = cell.Width * cell.ColumnSpan;
for (int ii = 1; ii < cell.ColumnSpan; ii++)
{
row.Cells[i + ii].Visible = false;
row.Cells[i + ii].VisibleState = true;
}
}
//如果不是第一行,并且左边与上边的单元格属性为隐藏,那么隐藏单元格
if (rowIndex != 0 && i > 0)
{
if (row.Cells[i - 1].Visible == false && row.Cells[i - 1].VisibleState == false && tb.Rows[rowIndex - 1].Cells[i].Visible == false && tb.Rows[rowIndex - 1].Cells[i].VisibleState == true)
{
cell.Visible = false;
}
}
}
}
}
#endregion
}
#endregion
#region 表对象
public class MyTable
{
public int Top { get; set; }
public int Left { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public bool ShowLine { get; set; }
public string FontColor { get; set; }
public string FontName { get; set; }
public float FontSize { get; set; }
public bool IsItalic { get; set; } //是否倾斜
public bool IsBold { get; set; } //是否加粗
public float BorderWidth { get; set; }
public List<MyRow> Rows { get; set; }
public List<MyColumn> Columns { get; set; }
public MyTable()
{
Rows = new List<MyRow>();
Columns = new List<MyColumn>();
BorderWidth = 1;
ShowLine = true;
FontName = "宋体";
FontColor = "Black";
FontSize = 12;
}
}
#endregion
#region 表格中的行
public class MyRow
{
public List<MyCell> Cells { get; set; }
public int Height { get; set; }
public string BackColor { get; set; }
public MyRow()
{
Cells = new List<MyCell>();
BackColor = "";
}
}
#endregion
#region 表格中的单元格
public class MyCell
{
public int Width { get; set; }
public int Height { get; set; }
public string FontColor { get; set; }
public string FontName { get; set; }
public float FontSize { get; set; }
public bool IsItalic { get; set; } //是否倾斜
public bool IsBold { get; set; } //是否加粗
public string Text { get; set; }
public string TextAlign { get; set; }
public string VerticalAlign { get; set; }
public bool Visible { get; set; }
public string BackColor { get; set; }
public MyImage Img { get; set; }
public int ColumnSpan { get; set; }
public int RowSpan { get; set; }
public Font font { get; set; }
/// <summary>
/// true为列隐藏,false为行隐藏
/// </summary>
public bool VisibleState { get; set; }
public MyCell()
{
FontColor = "";
BackColor = "";
FontName = "";
FontSize = 0;
TextAlign = "Center"; //文本方向,Left,Right,Center
VerticalAlign = "Center"; //Top,Center,Bottom
Text = "";
Visible = true;
ColumnSpan = 1;
RowSpan = 1;
}
}
#endregion
#region 表格中的列
public class MyColumn
{
public int Width { get; set; }
}
#endregion
#region 我的文字
public class MyString
{
public int Top { get; set; }
public int Left { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public string FontColor { get; set; }
public string FontName { get; set; }
public float FontSize { get; set; }
public bool IsItalic { get; set; } //是否倾斜
public bool IsBold { get; set; } //是否加粗
public string Text { get; set; }
public string TextAlign { get; set; }
public string VerticalAlign { get; set; }
public MyString()
{
FontName = "宋体";
FontColor = "Black";
FontSize = 12;
TextAlign = "Left"; //文本方向,Left,Right,Center
VerticalAlign = "Top"; //Top,Center,Bottom
}
}
#endregion
#region 我的图片
public class MyImage
{
public int Top { get; set; }
public int Left { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public string FilePath { get; set; }
}
#endregion
}