截图--控件截图
//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");
读取指纹文件图片,加载在picturebox中
/// <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();
bmp转png 设置透明度
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"); }
bmp图片缩放问题
/// <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); }
标签:targetSize,++,pbx,Height,Width,绘图,new,GDI,originalSize From: https://www.cnblogs.com/1175429393wljblog/p/18155072