private void CreateImageDocument(Image image, long imageSize)
 {
     document =  new ImageDoc() {
         Name = nameTextBox.Text,
         ImageData = ImageConversion.ImageToBase64(image, ImageFormat.Jpeg),
         Width = image.Width,
         Height = image.Height,
         Size = imageSize
     };
 }
 private void CreateImageAnnotation(ImageDoc.Annotation annotation)
 {
     // Create this or the drawing doesn't work !
     IMAGES.BaseAnnotation baseAnnotation =
         TranslateAnnotation.Translate(
                 annotation,
                 1.0f,
                 new IMAGES.ColourFormat(IMAGES.ColourFormat.COLOUR_FORMAT_32_BIT_COLOR),
                 new PointF(0, 0),
                 new PointF(0, 0)
         );
     baseAnnotation.Rect = new RectangleF(0, 0, baseAnnotation.Rect.Width, baseAnnotation.Rect.Height);
     baseAnnotation.UpdateScreenRect();
     imageEditorFrame.ImageEditor.Changes.Add(baseAnnotation);
 }
		/// <summary>
		/// Gets templates asynchronously (note the "await" keyword)
		/// "ConfigureAwait(false)" won't continue to use the current thread for 
		/// susequent processes (causes hang otherwise)
		/// </summary>
		/// <returns></returns>
		public async Task<List<ImageDoc>> GetListAsync(ImageDoc.ImageFilter filter) {
			var collection = session.Database.GetCollection<ImageDoc>(collectionName);
			var sort = Builders<ImageDoc>.Sort.Ascending(i => i.Name);
			var builder = Builders<ImageDoc>.Filter;
			var filterLike = builder.Regex("name", @"/" + filter.Name + @"/");
			if (filter.Width > 0) {
				filterLike = filterLike & builder.Gt("width", filter.Width);
			}
			if (filter.Height > 0) {
				filterLike = filterLike & builder.Gt("height", filter.Height);
			}
			if (filter.Size > 0) {
				filterLike = filterLike & builder.Gt("size", filter.Height);
			}
			return await collection.Find<ImageDoc>(filterLike).ToListAsync().ConfigureAwait(false);
		}
 /// <summary>
 /// Creates a new batch of images from the images specified in the file list
 /// </summary>
 /// <param name="patientNumber">Patient unique identifier</param>
 /// <param name="sourceTable">The entity to which the batch will belong (e.g. "lvEpisode")</param>
 /// <param name="fileNames">A list of file names of images to add to the batch</param>
 /// <returns>a new image batch contaiing the specified images</returns>
 public static List<ImageDoc> CreateNewBatchOfTestImages(string patientNumber, string sourceTable, List<string> fileNames)
 {
     // Create a new image batch
     List<ImageDoc> imageBatch = new List<ImageDoc>();
     // add images from the file list into the batch
     foreach(string fileName in fileNames) {
         // get the byte data for the file
         //byte[] data = GetTestImage(fileName);
         Image selectedImage = Image.FromFile(fileName);
         FileInfo info = new FileInfo(fileName);
         // create a new image document
         ImageDoc newImage = new ImageDoc();
         newImage.Annotations = new List<ImageDoc.Annotation>();
         newImage.ImageData = ImageConversion.ImageToBase64(selectedImage, ImageFormat.Jpeg);
         newImage.Name = info.Name;
         newImage.Width = selectedImage.Width;
         newImage.Height = selectedImage.Height;
         newImage.Size = info.Length;
         // add the new image document to the batch
         imageBatch.Add(newImage);
     }
     // the new image batch
     return imageBatch;
 }
		/// <summary>
		/// Save an image document
		/// </summary>
		/// <returns></returns>
		public void Save(ImageDoc doc) {
			ImageDocActions imageActions = new ImageDocActions(this.explorer);
			ReplaceOneResult result = imageActions.Save(doc);
			if (result.UpsertedId != null) {
				doc.Id = result.UpsertedId.AsObjectId;
			}
		}
		/// <summary>
		/// Get all images from the database
		/// </summary>
		/// <returns></returns>
		public async Task<List<ImageDoc>> GetImagesAsync(ImageDoc.ImageFilter filter) {
			ImageDocActions imageActions = new ImageDocActions(this.explorer);
			return await imageActions.GetListAsync(filter).ConfigureAwait(false);
		}
        public static BaseAnnotation Translate(ImageDoc.Annotation annotation, float scale, ColourFormat colourFormat, PointF controlOffset, PointF imageOffset)
        {
            BaseAnnotation imagingAnnotation = null;
            ImageDoc.ImagingAnnotationTypes annType = (ImageDoc.ImagingAnnotationTypes)
                Enum.Parse(typeof(ImageDoc.ImagingAnnotationTypes), annotation.AnnotationType);
            switch (annType) {
                case ImageDoc.ImagingAnnotationTypes.FreeLine: {
                    imagingAnnotation
                        = new FreeLineAnnotation(SimpleTypeToColor(annotation.LineColor), annotation.LineThickness, scale, colourFormat, controlOffset, imageOffset);
                    break;

                }
                case ImageDoc.ImagingAnnotationTypes.Highlighter: case ImageDoc.ImagingAnnotationTypes.Polygon: {
                    imagingAnnotation = new PolygonAnnotation(SimpleTypeToColor(annotation.LineColor), annotation.LineThickness, scale, colourFormat, controlOffset, imageOffset);
                }
                    break;
                case ImageDoc.ImagingAnnotationTypes.Stamp: {
                        imagingAnnotation = new StampAnnotation(annotation.LineThickness, scale, colourFormat, controlOffset, imageOffset);
                    }
                    break;
                case ImageDoc.ImagingAnnotationTypes.StraightLine: {
                    imagingAnnotation = new StraightLineAnnotation(SimpleTypeToColor(annotation.LineColor), annotation.LineThickness, scale, colourFormat, controlOffset, imageOffset);
                    }
                    break;
                case ImageDoc.ImagingAnnotationTypes.TextAnnotation: {
                    // Top left of the text
                    Point topLeft = new Point((int)annotation.Rect.TopLeft.X, (int)annotation.Rect.TopLeft.Y);
                    imagingAnnotation = new TextAnnotation(topLeft, controlOffset, imageOffset, scale, colourFormat);
                    }
                    break;
                case ImageDoc.ImagingAnnotationTypes.Svg: {
                        // Top left of the text
                    Point topLeft = new Point((int)annotation.Rect.TopLeft.X, (int)annotation.Rect.TopLeft.Y);
                        imagingAnnotation = new SvgAnnotation(topLeft, controlOffset, imageOffset, scale, colourFormat);
                        (imagingAnnotation as SvgAnnotation).ImageName = annotation.SvgImageName;
                    }
                    break;
            }
            if ((imagingAnnotation is PolygonAnnotation)) {
                (imagingAnnotation as PolygonAnnotation).ShapeName = annotation.SvgImageName;
                foreach (ImageDoc.Point point in annotation.Points) {
                    Point intPoint = new Point((int)point.X, (int)point.Y);
                    (imagingAnnotation as PolygonAnnotation).AddPoint(intPoint);
                }
                // This forces the initialisation of some required objects
                (imagingAnnotation as PolygonAnnotation).EndDrawing();
            }
            if ((imagingAnnotation is StampAnnotation)) {
                foreach (ImageDoc.Point point in annotation.Points) {
                    Point intPoint = new Point((int)point.X, (int)point.Y);
                    (imagingAnnotation as StampAnnotation).Points.Add(intPoint);
                }
            }
            if ((imagingAnnotation is TextAnnotation)) {
                (imagingAnnotation as TextAnnotation).Text = annotation.Text;
                (imagingAnnotation as TextAnnotation).TextFont = annotation.Font.ToFont();
                (imagingAnnotation as TextAnnotation).TextColor = SimpleTypeToColor(annotation.TextColor);
            }
            imagingAnnotation.Rect = SimpleTypeToRect(annotation.Rect);
            imagingAnnotation.Filled = true;
            imagingAnnotation.FillColor = SimpleTypeToColor(annotation.FillColor);
            imagingAnnotation.FillOpacity = annotation.FillOpacity;
            //imagingAnnotation.Outline = annotation.Outline;
            return imagingAnnotation;
        }
 private static RectangleF SimpleTypeToRect(ImageDoc.BoundingRect boundingRect)
 {
     RectangleF rect = new RectangleF(
         SimpleTypeToPoint(boundingRect.TopLeft),
         new SizeF(boundingRect.Width, boundingRect.Height)
     );
     return rect;
 }
 private static PointF SimpleTypeToPoint(ImageDoc.Point colorType)
 {
     return new PointF(colorType.X, colorType.Y);
 }
 private static Color SimpleTypeToColor(ImageDoc.ColorType colorType)
 {
     return Color.FromArgb(colorType.R, colorType.G, colorType.B);
 }
		public ReplaceOneResult Save(ImageDoc item) {
			var result = this.SaveAsync(item).Result;
			return result;
		}
		/// <summary>
		/// Save the image document, replacing the original.
		/// NOTE: Had many issues with this. Setting the "_id" column in code before inserting.
		/// Doesn't seem to ever ignore the default value of "ObjectId" 
		/// even with the 
		/// </summary>
		/// <param name="doc"></param>
		/// <returns></returns>
		private async Task<ReplaceOneResult> SaveAsync(ImageDoc doc) {
			//var collectionSettings = new MongoCollectionSettings { AssignIdOnInsert = false };
			var collection = session.Database.GetCollection<ImageDoc>(collectionName);
			return await collection.ReplaceOneAsync(
				filter: p => p.Id == doc.Id, 
				options: new UpdateOptions { IsUpsert = true },
				replacement: doc
				).ConfigureAwait(false);
		}