//control.CopyFromScreen //ok //Rectangle rectangleBounds = pbx.Bounds; //Bitmap bit = new Bitmap(rectangleBounds.Width, rectangleBounds.Height);//實例化一個和窗體一樣大的bitmap //Graphics g = Graphics.FromImage(bit); //g.CompositingQuality = CompositingQuality.HighQuality;//質量設爲最高 ////g.CopyFromScreen(pbx.Left, pbx.Top, 0, 0, new Size(pbx.Width, pbx.Height));//保存整個窗體爲圖片 //g.CopyFromScreen(pbx.PointToScreen(Point.Empty), Point.Empty, pbx.Size);//保存整個窗體爲圖片 ////g.CopyFromScreen(panel遊戲區 .PointToScreen(Point.Empty), Point.Empty, panel遊戲區.Size);//只保存某個控件(這裏是panel遊戲區) //bit.Save("簽名.png");//默認保存格式爲PNG,保存成jpg格式質量不是很好 //////保存dataGridView1截圖 //Bitmap newbitmap = new Bitmap(pbx.Width, pbx.Height); //pbx.DrawToBitmap(newbitmap, new Rectangle(0, 0, newbitmap.Width, newbitmap.Height)); //newbitmap.Save("簽名pbx.DrawToBitmap.png");
/// <summary> /// 創建畫布的畫板背景 /// </summary> Bitmap backgroundImage = null; //this.pbx.BackgroundImage = System.Drawing.Image.FromFile(tempPath); using (FileStream ms = File.OpenRead(tempPath)) { //File.WriteAllBytes("Fingerprint222.bmp", File.ReadAllBytes("11.png")); //File.WriteAllBytes("Fingerprint.bmp", imageBufFingerprint); //解決內存異常問題,以及this.pbx.BackgroundImage = System.Drawing.Image.FromStream(ms)導致的一般性gdi+ 問題 using (Bitmap bt = new Bitmap(ms)) { backgroundImage = new Bitmap(bt.Width, bt.Height); //Graphics g = pbx.CreateGraphics(); Graphics g = Graphics.FromImage(backgroundImage); //g.DrawLine(Pens.Black, startPoint, e.Location); //g.Clear(Color.White); //Pen myPen = new Pen(Color.Black, int.TryParse(ConfigurationManager.AppSettings["penwidth"], out int penwidth) ? penwidth : 3); g.SmoothingMode = SmoothingMode.AntiAlias; g.CompositingQuality = CompositingQuality.HighQuality; g.CompositingMode = CompositingMode.SourceOver; g.DrawImage(bt, bt.Width, bt.Height); //PointF pointFstart = PointToPointF(startPoint); //PointF pointFend = PointToPointF(e.Location); //g.DrawBeziers(myPen, new PointF[] { pointFstart, pointFend }); pbx.BackgroundImage = backgroundImage; g.Dispose(); //bt.Save("Fingerprint.jpg"); //this.pbx.BackgroundImage = System.Drawing.Image.FromStream(ms); } } //解決提示內存異常錯誤 GC.Collect();
var imagezzRaw2Bmp=System.IO.File.ReadAllBytes("Fingerprint.bmp"); using (Bitmap bitmap = new Bitmap(new MemoryStream(imagezzRaw2Bmp))) //using (Bitmap bitmap = new Bitmap("Fingerprint.bmp")) { //bitmap.MakeTransparent(Color.FromArgb(0, Color.Transparent)); bitmap.MakeTransparent(Color.FromArgb(0, Color.White));//設置指定顏色爲透明。白色 bitmap.Save("Fingerprint.png"); //var bitmap1 = Untils.GeneralConvert(bitmap, Color.Blue); //bitmap1.Save(@"01.png"); }
/// <summary> /// 縮小比例 /// </summary> /// <param name="originalSize"></param> /// <param name="targetSize"></param> /// <returns></returns> public static Size CalculateNewSize(Size originalSize, Size targetSize) { int newWidth, newHeight; // 如果原始寬度大於目標寬度,則按寬度比例縮放 if (originalSize.Width > targetSize.Width) { newWidth = targetSize.Width; newHeight = (int)(((float)originalSize.Height) * (float)((float)targetSize.Width / (float)originalSize.Width)); } // 如果原始高度大於目標高度,則按高度比例縮放 else if (originalSize.Height > targetSize.Height) { newHeight = targetSize.Height; newWidth = originalSize.Width * targetSize.Height / originalSize.Height; } // 如果原始尺寸小於或等於目標尺寸,則不需要縮放 else { newWidth = originalSize.Width; newHeight = originalSize.Height; } return new Size(newWidth, newHeight); } /// <summary> /// 放大縮小 /// </summary> /// <param name="originalSize"></param> /// <param name="targetSize"></param> /// <param name="isScale"></param> /// <returns></returns> public static Size CalculateSize(Size originalSize, Size targetSize, bool isScale = true) { int newWidth, newHeight; if (isScale) { // 如果原始寬度大於目標寬度,則按寬度比例縮放 if (originalSize.Width > targetSize.Width) { newWidth = targetSize.Width; newHeight = originalSize.Height * targetSize.Width / originalSize.Width; } // 如果原始高度大於目標高度,則按高度比例縮放 else if (originalSize.Height > targetSize.Height) { newHeight = targetSize.Height; newWidth = originalSize.Width * targetSize.Height / originalSize.Height; } // 如果原始尺寸小於或等於目標尺寸,則不需要縮放 else { newWidth = originalSize.Width; newHeight = originalSize.Height; } } else { // 如果原始寬度大於目標寬度,則按寬度比例放大 if (originalSize.Width < targetSize.Width) { newWidth = targetSize.Width; newHeight = targetSize.Height * targetSize.Width / originalSize.Width; } // 如果原始高度大於目標高度,則按高度比例縮放 else if (originalSize.Height < targetSize.Height) { newHeight = targetSize.Height; newWidth = originalSize.Width * targetSize.Height / originalSize.Height; } // 如果原始尺寸小於或等於目標尺寸,則不需要縮放 else { newWidth = originalSize.Width; newHeight = originalSize.Height; } } return new Size(newWidth, newHeight); } 調用 //縮小保存 int signWidth = int.TryParse(ConfigurationManager.AppSettings["fingerprintWidth"], out int signwidth) ? signwidth : 25; int signHeight = int.TryParse(ConfigurationManager.AppSettings["fingerprintHeight"], out int signheight) ? signheight : 36; // 計算等比縮放後的尺寸 Size newSize = Untils.CalculateNewSize(pbxFingerprint.Image.Size, new Size(signWidth, signHeight)); using (Bitmap resizedImage = new Bitmap(pbxFingerprint.Image, newSize)) { // 獲取PNG圖像編碼器 ImageCodecInfo imageCodecInfo = Untils.GetEncoderInfo("image/png"); //ImageCodecInfo.GetImageEncoders().Where(a=>a.MimeType== "image/png").First(); // 設置PNG圖像編碼參數 EncoderParameters encoderParameters = new EncoderParameters(1); EncoderParameter encoderParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); // 設置質量爲100% encoderParameters.Param[0] = encoderParameter; // 保存Bitmap爲PNG格式 //bitmap.MakeTransparent(Color.FromArgb(0, Color.Transparent)); resizedImage.MakeTransparent(Color.FromArgb(0, Color.White));//設置指定顏色爲透明。白色 resizedImage.Save(tempLocalFingerPrintPath, imageCodecInfo, encoderParameters); }