public override void OnDestroy() { base.OnDestroy(); // force our image view to release its references to our bitmaps ImageView.SetImageBitmap(null); // free the resources we're done with if (SourceImage != null) { SourceImage.Dispose( ); SourceImage = null; } if (ScaledCroppedImage != null) { ScaledCroppedImage.Dispose( ); ScaledCroppedImage = null; } if (ScaledSourceImage != null) { ScaledSourceImage.Dispose( ); ScaledSourceImage = null; } // free the cropped image if (CroppedImage != null) { CroppedImage.Dispose( ); CroppedImage = null; } SetMode(CropMode.None); }
public async Task <ScreenshotResult> Screenshot(string URL, string DirectoryPath, bool IncludeLogoWatermark, string WatermarkFilePath, Point CropStartPoint, Size CropSize, bool DoCropImage) { if (LoadedFine) { try { string ImagePath = DirectoryPath + "Screenshot-" + DateTime.UtcNow.Month.ToString() + "-" + DateTime.UtcNow.Day.ToString() + "-" + DateTime.UtcNow.Year.ToString() + "-" + DateTime.UtcNow.Hour.ToString() + "h" + DateTime.UtcNow.Minute.ToString() + "m" + DateTime.UtcNow.Second.ToString() + "s.png"; driver.Navigate() .GoToUrl(URL); Thread.Sleep(3000); var bytearray = driver.GetScreenshot().AsByteArray; //driver.Quit(); Bitmap bmp; using (var ms = new MemoryStream(bytearray)) { bmp = new Bitmap(ms); } Bitmap CroppedImage; if (DoCropImage) { Rectangle section = new Rectangle(CropStartPoint, CropSize); CroppedImage = CropImage(bmp, section); } else { CroppedImage = bmp; } if (IncludeLogoWatermark) { Bitmap Logo; if (WatermarkFilePath != "") { if (File.Exists(WatermarkFilePath)) { Logo = new Bitmap(WatermarkFilePath); } else { Logo = Properties.Resources.Watermark; } } else { Logo = Properties.Resources.Watermark; } Bitmap FinalLogo = ResizeBitmap(Logo, Logo.Width / 3, Logo.Height / 3); Graphics gCroppedImage = Graphics.FromImage(CroppedImage); gCroppedImage.DrawImage(FinalLogo, CroppedImage.Width / 2 - FinalLogo.Width / 2, 70, new Rectangle(0, 0, FinalLogo.Width, FinalLogo.Height), GraphicsUnit.Pixel); CroppedImage.Save(ImagePath, ImageFormat.Png); gCroppedImage.Dispose(); FinalLogo.Dispose(); Logo.Dispose(); } else { CroppedImage.Save(ImagePath, ImageFormat.Png); } CroppedImage.Dispose(); bmp.Dispose(); return(new ScreenshotResult(true, ImagePath)); } catch (Exception ex) { return(new ScreenshotResult(false, "")); } } else { return(new ScreenshotResult(false, "")); } }
void SetMode(CropMode mode) { if (mode == Mode) { throw new Exception(string.Format("Crop Mode {0} requested, but already in that mode.", mode)); } switch (mode) { case CropMode.Editing: { // If we're entering edit mode for the first time if (Mode == CropMode.None) { // Animate in the mask SimpleAnimator_Float floatAnimator = new SimpleAnimator_Float(MaskLayer.Opacity, MaskFadeAmount, MaskFadeTime, delegate(float percent, object value) { Rock.Mobile.Threading.Util.PerformOnUIThread(delegate { MaskLayer.Opacity = (float)value; }); }, null); floatAnimator.Start( ); // turn on our cropper CropView.Visibility = ViewStates.Visible; // and set the source image to the scaled source. ImageView.SetImageBitmap(ScaledSourceImage); } // else we're coming FROM Preview Mode, so we need to animate else { Animating = true; // setup the dimension changes System.Drawing.SizeF startSize = new System.Drawing.SizeF(ImageView.Width, ImageView.Height); System.Drawing.SizeF endSize = new System.Drawing.SizeF(CropView.Width, CropView.Height); PointF startPos = new PointF(ImageView.GetX( ), ImageView.GetY( )); PointF endPos = new PointF(CropView.GetX( ), CropView.GetY( )); // now animate the cropped image up to its full size AnimateImageView(ImageView, startPos, endPos, startSize, endSize, ImageAnimationTime, delegate { ImageView.SetImageBitmap(null); // release any cropped image we had. if (CroppedImage != null) { CroppedImage.Dispose( ); CroppedImage = null; } // release the scaled version if we had it if (ScaledCroppedImage != null) { ScaledCroppedImage.Dispose( ); ScaledCroppedImage = null; } ImageView.SetImageBitmap(ScaledSourceImage); ImageView.LayoutParameters.Width = ScaledSourceImage.Width; ImageView.LayoutParameters.Height = ScaledSourceImage.Height; // center the image ImageView.SetX((ScreenSize.Width - ImageView.LayoutParameters.Width) / 2); ImageView.SetY((ScreenSize.Height - ImageView.LayoutParameters.Height) / 2); MaskLayer.Visibility = ViewStates.Visible; CropView.Visibility = ViewStates.Visible; SimpleAnimator_Float floatAnimator = new SimpleAnimator_Float(MaskLayer.Opacity, MaskFadeAmount, MaskFadeTime, delegate(float percent, object value) { Rock.Mobile.Threading.Util.PerformOnUIThread(delegate { MaskLayer.Opacity = (float)value; CropView.Alpha = percent; }); }, // FINISHED MASK FADE-OUT delegate { Rock.Mobile.Threading.Util.PerformOnUIThread(delegate { Animating = false; }); }); floatAnimator.Start( ); }); } break; } case CropMode.Previewing: { // don't allow a state change while we're animating Animating = true; SimpleAnimator_Float floatAnimator = new SimpleAnimator_Float(MaskLayer.Opacity, 1.00f, MaskFadeTime, delegate(float percent, object value) { Rock.Mobile.Threading.Util.PerformOnUIThread(delegate { MaskLayer.Opacity = (float)value; CropView.Alpha = 1.0f - percent; }); }, // FINISHED MASK FADE-IN delegate { Rock.Mobile.Threading.Util.PerformOnUIThread(delegate { // hide the mask and cropper MaskLayer.Visibility = ViewStates.Gone; CropView.Visibility = ViewStates.Gone; // create the cropped image CroppedImage = CropImage(SourceImage, new System.Drawing.RectangleF(CropView.GetX( ) - CropViewMinPos.X, CropView.GetY( ) - CropViewMinPos.Y, CropView.LayoutParameters.Width, CropView.LayoutParameters.Height)); // create a scaled version of the cropped image float scaledWidth = (float)CroppedImage.Width * (1.0f / ScreenToImageScalar); float scaledHeight = (float)CroppedImage.Height * (1.0f / ScreenToImageScalar); ScaledCroppedImage = Bitmap.CreateScaledBitmap(CroppedImage, (int)scaledWidth, (int)scaledHeight, false); // set the scaled cropped image ImageView.SetImageBitmap(null); ImageView.SetImageBitmap(ScaledCroppedImage); // start the scaled cropped image scaled down further to match its size within the full image. ImageView.SetX(CropView.GetX( )); ImageView.SetY(CropView.GetY( )); ImageView.LayoutParameters.Width = CropView.Width; ImageView.LayoutParameters.Height = CropView.Height; // setup the dimension changes System.Drawing.SizeF startSize = new System.Drawing.SizeF(ScaledCroppedImage.Width, ScaledCroppedImage.Height); System.Drawing.SizeF endSize; if (ScreenSize.Width < ScreenSize.Height) { endSize = new System.Drawing.SizeF(ScreenSize.Width, (float)System.Math.Ceiling(ScreenSize.Width * (startSize.Width / startSize.Height))); } else { endSize = new System.Drawing.SizeF((float)System.Math.Ceiling(ScreenSize.Height * (startSize.Height / startSize.Width)), ScreenSize.Height); } PointF startPos = new PointF(CropView.GetX( ), CropView.GetY( )); PointF endPos = new PointF((ScreenSize.Width - endSize.Width) / 2, (ScreenSize.Height - endSize.Height) / 2); // now animate the cropped image up to its full size AnimateImageView(ImageView, startPos, endPos, startSize, endSize, ImageAnimationTime, delegate { Animating = false; }); }); }); floatAnimator.Start( ); break; } } Mode = mode; }
/// <summary> /// This is testting Face module /// </summary> public async Task <String> Testpicture(String testImageFile) { try { People people = new People(); List <string> rslist = new List <string>(); string[] HeadRandom; StringBuilder Mount_path = new StringBuilder(); FaceServiceClient fc = new FaceServiceClient(ApiKey, "https://southeastasia.api.cognitive.microsoft.com/face/v1.0"); string personGroupId = "test"; // await fc.CreatePersonGroupAsync(personGroupId,"My Family"); //using (Stream s = File.OpenRead(testImageFile)) //{ // if (s != null) // return "yes"; // else // return "no"; //} using (Stream s = File.OpenRead(testImageFile)) { var requiredFaceAttributes = new FaceAttributeType[] { FaceAttributeType.Age, FaceAttributeType.Gender, FaceAttributeType.Smile, FaceAttributeType.FacialHair, FaceAttributeType.HeadPose, FaceAttributeType.Glasses, FaceAttributeType.Emotion }; var faces = await fc.DetectAsync(s, returnFaceLandmarks : true, returnFaceAttributes : requiredFaceAttributes); var faceIds = faces.Select(face => face.FaceId).ToArray(); try { var results = await fc.IdentifyAsync(personGroupId, faceIds); var fspicture = new FileStream(testImageFile, FileMode.Open); Bitmap bmp = new Bitmap(fspicture); Graphics g = Graphics.FromImage(bmp); int isM = 0, isF = 0; // string age = ""; string sex = ""; int age; String age_s = ""; String emr = ""; String Top_Emotion = ""; Dictionary <string, float> Emotion = new Dictionary <string, float>(); foreach (var face in faces) { var faceRect = face.FaceRectangle; var attributes = face.FaceAttributes; float Happiness = attributes.Emotion.Happiness; float Anger = attributes.Emotion.Anger; float Neutral = attributes.Emotion.Neutral; float Contempt = attributes.Emotion.Contempt; float Disgust = attributes.Emotion.Disgust; float Fear = attributes.Emotion.Fear; float Sadness = attributes.Emotion.Sadness; float Surprise = attributes.Emotion.Surprise; String[] Emotion_string = { "Anger", "Happiness", "Neutral", "Contempt", "Disgust", "Fear", "Sadness", "Surprise" }; float[] Emotion_array = { Anger, Happiness, Neutral, Contempt, Disgust, Fear, Sadness, Surprise }; // g.DrawEllipse(new Pen(Brushes.Blue, 5), new System.Drawing.Rectangle(faceRect.Left-90, faceRect.Top-90, // faceRect.Width+150, faceRect.Height+150)); /* g.DrawRectangle( * new Pen(Brushes.Red, 3), * new System.Drawing.Rectangle(faceRect.Left, faceRect.Top, * faceRect.Width, faceRect.Height));*/ //g.DrawString(new Font(attributes.Gender.ToString(),)); for (int i = 0; i < Emotion_string.Length; i++) { Emotion.Add(Emotion_string[i], Emotion_array[i]); } if (attributes.Gender.StartsWith("male")) { isM += 1; } else { isF += 1; } age = Convert.ToInt32(attributes.Age); age_s = age.ToString(); sex = attributes.Gender.ToString(); Top_Emotion = GetEmotion(attributes.Emotion); Console.WriteLine(Top_Emotion); //Font drawFont = new Font("Arial", 60, FontStyle.Bold); //SolidBrush drawBrush = new SolidBrush(Color.Blue); //PointF drawPoint = new PointF(faceRect.Left - 90, faceRect.Top - 50); //PointF drawPoint2 = new PointF(faceRect.Left - 10, faceRect.Top - 50); //g.DrawString(age_s, drawFont, drawBrush, drawPoint); //g.DrawString(sex, drawFont, drawBrush, drawPoint2); Bitmap CroppedImage = null; if (face.FaceAttributes.HeadPose.Roll >= 10 || face.FaceAttributes.HeadPose.Roll <= -10) { System.Drawing.Rectangle rect = new System.Drawing.Rectangle(Convert.ToInt32(face.FaceRectangle.Left - 200), Convert.ToInt32(face.FaceRectangle.Top - 200), face.FaceRectangle.Width + 200, face.FaceRectangle.Height + 200); CroppedImage = new Bitmap(CropRotatedRect(bmp, rect, Convert.ToSingle(face.FaceAttributes.HeadPose.Roll * -1), true)); } else { try { CroppedImage = new Bitmap(bmp.Clone(new System.Drawing.Rectangle(face.FaceRectangle.Left - 150, face.FaceRectangle.Top - 150, face.FaceRectangle.Width + 300, face.FaceRectangle.Height + 300), bmp.PixelFormat)); } catch (Exception e) { } } bmp.Dispose(); CroppedImage.Save(@"C:\Users\v-altsai\Pictures\allfix.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); CroppedImage.Dispose(); Console.WriteLine("Age " + age); } Console.WriteLine("Female: " + isF); Console.WriteLine("Male: " + isM); String name = ""; foreach (var identifyResult in results) { // Console.WriteLine("Result of face: {0}", identifyResult.FaceId); if (identifyResult.Candidates.Length == 0) { Console.WriteLine("No one identified"); name = "none"; } else if (identifyResult.Candidates.Length != 0) { var candidateId = identifyResult.Candidates[0].PersonId; var person = await fc.GetPersonAsync(personGroupId, candidateId); Console.WriteLine("Identified as {0}", person.Name); name = person.Name; Bitmap oribmp = new Bitmap(@"C:\Users\v-altsai\Pictures\allfix.jpg"); using (Bitmap tmpBmp = new Bitmap(oribmp)) { Mount_path.Clear(); Mount_path.Append("C:\\Users\\v-altsai\\Pictures\\Family\\"); Mount_path.Append(person.Name); Mount_path.Append(".jpg"); tmpBmp.Save(Mount_path.ToString(), System.Drawing.Imaging.ImageFormat.Jpeg); Console.WriteLine("{0}", Mount_path.ToString()); } } } // bmp.Save(@"C:\Users\v-altsai\Documents\Visual Studio 2017\Projects\Kinect-FaceRecognition\Images\allfix.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); people.Name = name; people.Age = age_s; people.Gender = sex; people.Emotion = Top_Emotion; people.Emotionlistscore = Emotion; JSONHelper helper = new JSONHelper(); String jsonResult = ""; jsonResult = helper.ConvertObjectToJSon(people); fspicture.Close(); s.Close(); Emotion.Clear(); return(jsonResult); } catch (FaceAPIException fs) { Console.WriteLine(fs.ToString()); Console.WriteLine("error results"); //if (s != null) // s.Close(); return(null); } } } catch (Exception e) { String msg = "Oops! Something went wrong. Try again later"; if (e is ClientException && (e as ClientException).Error.Message.ToLowerInvariant().Contains("access denied")) { msg += " (access denied - hint: check your APIKEY )."; Console.Write(msg); } Console.Write(e.ToString()); return(null); } }