Skip to content

Commit 1b19b4c

Browse files
committed
支持调整透明度
优化印章旋转方式,以支持骑缝章旋转
1 parent c9cf6af commit 1b19b4c

File tree

2 files changed

+124
-23
lines changed

2 files changed

+124
-23
lines changed

PDFQFZ/Form1.Designer.cs

Lines changed: 60 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PDFQFZ/Form1.cs

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,49 @@ private void pdfGz()
184184
}
185185
}
186186

187+
public Bitmap RotateImg(Image b, int angle, string filename)
188+
{
189+
angle = angle % 360;
190+
//弧度转换
191+
double radian = angle * Math.PI / 180.0;
192+
double cos = Math.Cos(radian);
193+
double sin = Math.Sin(radian);
194+
//原图的宽和高
195+
int w = b.Width;
196+
int h = b.Height;
197+
int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
198+
int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
199+
200+
201+
//为了尽可能的去除白边,减小印章旋转后尺寸的误差,这里保持原印章宽度,切掉部分角
202+
H = (int)(1f * H * w / W);
203+
W = w;
204+
205+
206+
//目标位图
207+
Bitmap dsImage = new Bitmap(W, H);
208+
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);
209+
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
210+
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
211+
//计算偏移量
212+
Point Offset = new Point((W - w) / 2, (H - h) / 2);
213+
//构造图像显示区域:让图像的中心与窗口的中心点一致
214+
Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
215+
Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
216+
g.TranslateTransform(center.X, center.Y);
217+
g.RotateTransform(angle);
218+
//恢复图像在水平和垂直方向的平移
219+
g.TranslateTransform(-center.X, -center.Y);
220+
g.DrawImage(b, rect);
221+
//重至绘图的所有变换
222+
g.ResetTransform();
223+
g.Save();
224+
g.Dispose();
225+
//保存旋转后的图片
226+
dsImage.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
227+
return dsImage;
228+
}
229+
187230
//分割图片
188231
private static Bitmap[] subImages(String imgPath, int n)//图片分割
189232
{
@@ -210,15 +253,23 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath, X509Certi
210253
int zyz = comboYz.SelectedIndex;//加印章方式
211254
int zqfz = comboQfz.SelectedIndex;//加骑缝章方式
212255
int sftype = comboBoxBL.SelectedIndex;//印章缩放方式
213-
float sfsize, yzrotation, qfzwzbl;
256+
float sfsize,opacity, yzrotation, qfzwzbl;
214257
float.TryParse(textBili.Text, out sfsize);//印章缩放尺寸
215-
float.TryParse(textRotation.Text, out yzrotation);//骑缝章位置比例
258+
float.TryParse(textOpacity.Text, out opacity);//不透明度比例
259+
float.TryParse(textRotation.Text, out yzrotation);//印章旋转角度
216260
float.TryParse(textWzbl.Text, out qfzwzbl);//骑缝章位置比例
217261
float picbl = 1.003f;//别问我这个数值怎么来的
218262
float picmm = 2.842f;//别问我这个数值怎么来的
219263

264+
if (yzrotation != 0)
265+
{
266+
string filename = System.IO.Path.GetTempPath()+"PDFQFZ_tmp.png";
267+
RotateImg(new Bitmap(ModelPicName), (int)yzrotation, filename);
268+
ModelPicName = filename;
269+
}
270+
220271
PdfGState state = new PdfGState();
221-
state.FillOpacity = 0.6f;//印章图片整体透明度
272+
state.FillOpacity = opacity/100f;//印章图片不透明度
222273

223274
//throw new NotImplementedException();
224275
PdfReader pdfReader = null;
@@ -284,7 +335,7 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath, X509Certi
284335
}
285336
else
286337
{
287-
sfbl = 100f * sfsize * picmm / image.Height;
338+
sfbl = 100f * sfsize * picmm / (image.Width * tmp);//印章尺寸*DPI转换比例/(分割后图片的宽度*分割份数),再转百分比
288339
image.ScalePercent(sfbl);//设置图片比例
289340

290341
//sfbl = sfsize * picmm;//别问我为什么要乘这个
@@ -327,7 +378,7 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath, X509Certi
327378
{
328379
img = iTextSharp.text.Image.GetInstance(ModelPicName);//创建一个图片对象
329380
img.Transparency = new int[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };//这里透明背景的图片会变黑色,所以设置黑色为透明
330-
img.RotationDegrees = yzrotation;
381+
//img.RotationDegrees = yzrotation;
331382

332383
if (sftype == 0)
333384
{
@@ -339,7 +390,7 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath, X509Certi
339390
}
340391
else
341392
{
342-
sfbl = 100f*sfsize * picmm / img.Height;
393+
sfbl = 100f*sfsize * picmm / img.Width;
343394
img.ScalePercent(sfbl);//设置图片比例
344395
//sfbl = sfsize * picmm;//别问我为什么要乘这个
345396
//imgW = sfbl;
@@ -614,10 +665,13 @@ private void Form1_Load(object sender, EventArgs e)
614665
private void pictureBox1_Click(object sender, EventArgs e)
615666
{
616667
Point pt = pictureBox1.PointToClient(Control.MousePosition);
617-
float picw = pictureBox1.Width;
618-
float pich = pictureBox1.Height;
619-
float px = pt.X / picw;
620-
float py = pt.Y / pich;
668+
int picw = pictureBox1.Width;
669+
int pich = pictureBox1.Height;
670+
671+
if (pt.X > picw) pt.X = picw;
672+
if (pt.Y > pich) pt.Y = pich;
673+
float px = 1f * pt.X / picw;
674+
float py = 1f * pt.Y / pich;
621675
textPx.Text = px.ToString("#0.0000");
622676
textPy.Text = py.ToString("#0.0000");
623677
DataRow[] arrRow = dtPos.Select("Path = '"+ pdfInputPath + "' and Page = "+ imgStartPage);

0 commit comments

Comments
 (0)