public static PlotModel ImageAnnotation() { var model = new PlotModel("ImageAnnotation", "Click the image"); model.Axes.Add(new LinearAxis(AxisPosition.Bottom, -20, 20)); model.Axes.Add(new LinearAxis(AxisPosition.Left, -10, 10)); OxyImage image; var assembly = Assembly.GetExecutingAssembly(); using (var stream = assembly.GetManifestResourceStream("ExampleLibrary.Resources.OxyPlot.png")) { image = new OxyImage(stream); } var ia = new ImageAnnotation(image, new DataPoint(4, 2), HorizontalAlignment.Right); model.Annotations.Add(ia); // Handle left mouse clicks ia.MouseDown += (s, e) => { if (e.ChangedButton != OxyMouseButton.Left) { return; } ia.HorizontalAlignment = ia.HorizontalAlignment == HorizontalAlignment.Right ? HorizontalAlignment.Left : HorizontalAlignment.Right; model.InvalidatePlot(false); e.Handled = true; }; return(model); }
/// <summary> /// Gets the bitmap source. /// </summary> /// <param name="image">The image.</param> /// <returns>The bitmap source.</returns> private BitmapSource GetImageSource(OxyImage image) { if (image == null) { return(null); } if (!this.imagesInUse.Contains(image)) { this.imagesInUse.Add(image); } BitmapSource bitmapSource; if (this.imageCache.TryGetValue(image, out bitmapSource)) { return(bitmapSource); } var randomAccessStream = new InMemoryRandomAccessStream(); var writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0)); writer.WriteBytes(image.GetData()); writer.StoreAsync(); bitmapSource = new BitmapImage(); bitmapSource.SetSource(randomAccessStream); this.imageCache.Add(image, bitmapSource); return(bitmapSource); }
Bitmap GetImage(OxyImage source) { if (source == null) { return(null); } if (!this.imagesInUse.Contains(source)) { this.imagesInUse.Add(source); } Bitmap src; if (this.imageCache.TryGetValue(source, out src)) { return(src); } if (source != null) { Bitmap btm; // can't use Dispose because of async loading (or use OperationType.Sync) //using (var ms = new MemoryStream(source.GetData())) var ms = new System.IO.MemoryStream(source.GetData()); { btm = Bitmap.FromStream(ms, OperationType.Sync); } this.imageCache.Add(source, btm); return(btm); } return(null); }
/// <summary> /// Gets or creates a <see cref="XImage" /> from the specified image. /// </summary> /// <param name="source">The source.</param> /// <returns>The image</returns> private XImage GetImage(OxyImage source) { if (source == null) { return(null); } if (!this.imagesInUse.Contains(source)) { this.imagesInUse.Add(source); } XImage src; if (this.imageCache.TryGetValue(source, out src)) { return(src); } XImage bitmap; using (var ms = new MemoryStream(source.GetData())) { var im = System.Drawing.Image.FromStream(ms); bitmap = XImage.FromGdiPlusImage(im); } this.imageCache.Add(source, bitmap); return(bitmap); }
/// <summary> /// Updates the image. /// </summary> private void UpdateImage() { // determine if the provided data should be reversed in x-direction var reverseX = this.XAxis.Transform(this.X0) > this.XAxis.Transform(this.X1); // determine if the provided data should be reversed in y-direction var reverseY = this.YAxis.Transform(this.Y0) > this.YAxis.Transform(this.Y1); // determine if the data should be transposed var swapXY = this.IsTransposed(); int m = this.Data.GetLength(0); int n = this.Data.GetLength(1); var buffer = swapXY ? new OxyColor[n, m] : new OxyColor[m, n]; for (int i = 0; i < m; i++) { var ii = reverseX ? m - 1 - i : i; for (int j = 0; j < n; j++) { var jj = reverseY ? n - 1 - j : j; if (swapXY) { buffer[j, i] = this.ColorAxis.GetColor(this.Data[ii, jj]); } else { buffer[i, j] = this.ColorAxis.GetColor(this.Data[ii, jj]); } } } this.image = OxyImage.Create(buffer, ImageFormat.Png); }
/// <summary> /// Converts the specified <see cref="OxyImage" /> to a <see cref="PortableDocumentImage" />. /// </summary> /// <param name="image">The source image.</param> /// <param name="interpolate">interpolate if set to <c>true</c>.</param> /// <returns>The converted image.</returns> public static PortableDocumentImage Convert(OxyImage image, bool interpolate) { OxyColor[,] pixels; try { pixels = image.GetPixels(); } catch { // TODO: remove this try/catch block when image decoder is implemented. return null; } var bits = new byte[image.Width * image.Height * 3]; var maskBits = new byte[image.Width * image.Height]; int i = 0; int j = 0; // Start at the top row for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { maskBits[j++] = pixels[x, y].A; bits[i++] = pixels[x, y].R; bits[i++] = pixels[x, y].G; bits[i++] = pixels[x, y].B; } } return new PortableDocumentImage(image.Width, image.Height, 8, bits, maskBits, interpolate); }
/// <summary> /// Renders the series on the specified render context. /// </summary> /// <param name="rc">The rendering context.</param> /// <param name="model">The model.</param> public override void Render(IRenderContext rc, PlotModel model) { var p0 = this.Transform(this.XAxis.ActualMinimum, this.YAxis.ActualMinimum); var p1 = this.Transform(this.XAxis.ActualMaximum, this.YAxis.ActualMaximum); var w = (int)(p1.X - p0.X); var h = (int)(p0.Y - p1.Y); int maxIterations = (int)this.ColorAxis.ActualMaximum + 1; var pixels = new OxyColor[h, w]; ParallelFor( 0, h, i => { double y = this.YAxis.ActualMinimum + ((double)i / (h - 1) * (this.YAxis.ActualMaximum - this.YAxis.ActualMinimum)); for (int j = 0; j < w; j++) { double x = this.XAxis.ActualMinimum + ((double)j / (w - 1) * (this.XAxis.ActualMaximum - this.XAxis.ActualMinimum)); var iterations = Solve(x, y, maxIterations); pixels[i, j] = this.ColorAxis.GetColor((double)iterations); } }); var bitmap = OxyImage.PngFromArgb(pixels); //TEST rc.DrawImage(bitmap, p0.X, p1.Y, p1.X - p0.X, p0.Y - p1.Y, 1, true); RenderingExtensions.DrawImage(rc, bitmap, p0.X, p1.Y, p1.X - p0.X, p0.Y - p1.Y, 1, true); }
/// <summary> /// Renders the series on the specified render context. /// </summary> /// <param name="rc"> /// The rendering context. /// </param> /// <param name="model"> /// The model. /// </param> public override void Render(IRenderContext rc, PlotModel model) { if (this.Data == null) { this.image = null; return; } int m = this.Data.GetLength(0); int n = this.Data.GetLength(1); double dx = (this.X1 - this.X0) / m; double left = this.X0 - (dx * 0.5); double right = this.X1 + (dx * 0.5); double dy = (this.Y1 - this.Y0) / n; double bottom = this.Y0 - (dy * 0.5); double top = this.Y1 + (dy * 0.5); var s00 = this.Transform(left, bottom); var s11 = this.Transform(right, top); var rect = OxyRect.Create(s00, s11); if (this.image == null || this.Data.GetHashCode() != this.dataHash) { this.UpdateImage(); this.dataHash = this.Data.GetHashCode(); } if (this.image != null) { var clip = this.GetClippingRect(); rc.DrawClippedImage(clip, this.image, rect.Left, rect.Top, rect.Width, rect.Height, 1, true); } }
/// <inheritdoc/> public void DrawImage( OxyImage source, double srcX, double srcY, double srcWidth, double srcHeight, double destX, double destY, double destWidth, double destHeight, double opacity, bool interpolate) { if (source == null) { return; } var bytes = source.GetData(); var image = SKBitmap.Decode(bytes); var src = new SKRect((float)srcX, (float)srcY, (float)(srcX + srcWidth), (float)(srcY + srcHeight)); var dest = new SKRect(this.Convert(destX), this.Convert(destY), this.Convert(destX + destWidth), this.Convert(destY + destHeight)); var paint = this.GetImagePaint(opacity, interpolate); this.SkCanvas.DrawBitmap(image, src, dest, paint); }
/// <summary> /// Renders the icon on the specified render context. /// </summary> /// <param name="rc">The render context.</param> /// <param name="size">The size.</param> public override void Render(IRenderContext rc, double size) { var n = (int)size * 2; var data = ArrayHelper.Evaluate(Functions.Peaks, ArrayHelper.CreateVector(-3.1, 2.6, n), ArrayHelper.CreateVector(3.0, -3.4, n)); var c = OxyColor.FromArgb(255, 90, 165, 200); var palette = OxyPalette.Interpolate(5, OxyColors.White, c, OxyColors.White); var min = data.Min2D(); var max = data.Max2D(); var pixels = new OxyColor[n, n]; for (int x = 0; x < n; x++) { for (int y = 0; y < n; y++) { var i = (int)((data[x, y] - min) / (max - min) * palette.Colors.Count); i = Math.Min(Math.Max(i, 0), palette.Colors.Count - 1); pixels[y, n - 1 - x] = palette.Colors[i]; } } var image = OxyImage.Create(pixels, OxyPlot.ImageFormat.Png); // fix image interpolation artifacts on the edge rc.DrawImage(image, 0, 0, n, n, 0, 0, size, size, 1, true); rc.DrawRectangle(new OxyRect(0, 0, size, 2), c, OxyColors.Undefined, 0); rc.DrawRectangle(new OxyRect(0, size - 1, size, 1), c, OxyColors.Undefined, 0); rc.DrawRectangle(new OxyRect(0, 0, 2, size), c, OxyColors.Undefined, 0); rc.DrawRectangle(new OxyRect(size - 1, 0, 1, size), c, OxyColors.Undefined, 0); }
/// <summary> /// Renders the icon on the specified render context. /// </summary> /// <param name="rc">The render context.</param> /// <param name="size">The size.</param> public override void Render(IRenderContext rc, double size) { var n = (int)size * 2; var data = ArrayHelper.Evaluate(Functions.Peaks, ArrayHelper.CreateVector(-3.1, 2.6, n), ArrayHelper.CreateVector(3.0, -3.4, n)); var palette = OxyPalettes.BlueWhiteRed(5); var min = data.Min2D(); var max = data.Max2D(); var pixels = new OxyColor[n, n]; for (int x = 0; x < n; x++) { for (int y = 0; y < n; y++) { var i = (int)((data[x, y] - min) / (max - min) * palette.Colors.Count); i = Math.Min(Math.Max(i, 0), palette.Colors.Count - 1); pixels[y, n - 1 - x] = palette.Colors[i]; } } var image = OxyImage.Create(pixels, OxyPlot.ImageFormat.Png); rc.DrawImage(image, 0, 0, n, n, 0, 0, size, size, 1, true); var frameWidth = (int)Math.Max(Math.Round(size / 32), 1); rc.DrawRectangle(new OxyRect(0, 0, size, frameWidth), OxyColors.Black, OxyColors.Black, 0); rc.DrawRectangle(new OxyRect(0, size - frameWidth, size, frameWidth), OxyColors.Black, OxyColors.Undefined, 0); rc.DrawRectangle(new OxyRect(0, 0, frameWidth, size), OxyColors.Black, OxyColors.Undefined, 0); rc.DrawRectangle(new OxyRect(size - frameWidth, 0, frameWidth, size), OxyColors.Black, OxyColors.Undefined, 0); }
public static PlotModel ImageAnnotation() { var model = new PlotModel("ImageAnnotation", "Click the image"); model.Axes.Add(new LinearAxis(AxisPosition.Bottom, -20, 20)); model.Axes.Add(new LinearAxis(AxisPosition.Left, -10, 10)); OxyImage image = null; using (var stream = Alt.IO.VirtualFile.OpenRead("AltData/OxyPlot/OxyPlot.png")) { image = new OxyImage(stream); } var ia = new ImageAnnotation(image, new DataPoint(4, 2), global::OxyPlot.HorizontalAlignment.Right); model.Annotations.Add(ia); // Handle left mouse clicks ia.MouseDown += (s, e) => { if (e.ChangedButton != OxyMouseButton.Left) { return; } ia.HorizontalAlignment = ia.HorizontalAlignment == global::OxyPlot.HorizontalAlignment.Right ? global::OxyPlot.HorizontalAlignment.Left : global::OxyPlot.HorizontalAlignment.Right; model.InvalidatePlot(false); e.Handled = true; }; return(model); }
/// <summary> /// Gets the bitmap source. /// </summary> /// <param name="image">The image.</param> /// <returns>The bitmap source.</returns> private TextureSource GetImageSource(OxyImage image) { if (image == null) { return(null); } if (!this.imagesInUse.Contains(image)) { this.imagesInUse.Add(image); } TextureSource src; if (this.imageCache.TryGetValue(image, out src)) { return(src); } return(new TextureSource()); /* using (var ms = new MemoryStream(image.GetData())) * { * var btm = new BitmapImage(); * btm.BeginInit(); * btm.StreamSource = ms; * btm.CacheOption = BitmapCacheOption.OnLoad; * btm.EndInit(); * btm.Freeze(); * this.imageCache.Add(image, btm); * return btm; * }*/ }
/// <summary> /// Loads the image from the specified source. /// </summary> /// <param name="source">The image source.</param> /// <returns>A <see cref="Image" />.</returns> private Image GetImage(OxyImage source) { if (source == null) { return(null); } if (!this.imagesInUse.Contains(source)) { this.imagesInUse.Add(source); } Image src; if (this.imageCache.TryGetValue(source, out src)) { return(src); } Image btm; using (var ms = new MemoryStream(source.GetData())) { btm = Image.FromStream(ms); } this.imageCache.Add(source, btm); return(btm); }
internal static OxyImage DrawText(string text, double size) { Font f = new Font("Arial", (uint)size); Image img = new Bitmap(1, 1); Graphics drawing = Graphics.FromImage(img); var sz = drawing.MeasureString(text, f); img.Dispose(); drawing.Dispose(); img = new Bitmap((int)sz.Width, (int)sz.Height); drawing = Graphics.FromImage(img); drawing.Clear(Color.Transparent); Brush textBrush = new SolidBrush(Color.White); drawing.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; drawing.DrawString(text, f, textBrush, 0, 0); drawing.Save(); textBrush.Dispose(); drawing.Dispose(); var str = new MemoryStream(); img.Save(str, System.Drawing.Imaging.ImageFormat.Png); img.Dispose(); OxyImage res = new OxyImage(str.GetBuffer()); str.Close(); str.Dispose(); return(res); }
/// <summary> /// Converts the specified <see cref="OxyImage" /> to a <see cref="PortableDocumentImage" />. /// </summary> /// <param name="image">The source image.</param> /// <param name="interpolate">interpolate if set to <c>true</c>.</param> /// <returns>The converted image.</returns> public static PortableDocumentImage Convert(OxyImage image, bool interpolate) { OxyColor[,] pixels; try { pixels = image.GetPixels(); } catch { // TODO: remove this try/catch block when image decoder is implemented. return(null); } var bits = new byte[image.Width * image.Height * 3]; var maskBits = new byte[image.Width * image.Height]; int i = 0; int j = 0; // Start at the top row for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { maskBits[j++] = pixels[x, y].A; bits[i++] = pixels[x, y].R; bits[i++] = pixels[x, y].G; bits[i++] = pixels[x, y].B; } } return(new PortableDocumentImage(image.Width, image.Height, 8, bits, maskBits, interpolate)); }
/// <summary> /// Gets the bitmap source. /// </summary> /// <param name="image">The image.</param> /// <returns>The bitmap source.</returns> private BitmapSource GetImageSource(OxyImage image) { if (image == null) { return(null); } if (!this.imagesInUse.Contains(image)) { this.imagesInUse.Add(image); } BitmapSource src; if (this.imageCache.TryGetValue(image, out src)) { return(src); } using (var ms = new MemoryStream(image.GetData())) { var btm = new BitmapImage(); btm.BeginInit(); btm.StreamSource = ms; btm.CacheOption = BitmapCacheOption.OnLoad; btm.EndInit(); btm.Freeze(); this.imageCache.Add(image, btm); return(btm); } }
/// <summary> /// Draws the specified portion of the specified <see cref="OxyImage" /> at the specified location and with the specified size. /// </summary> /// <param name="source">The source.</param> /// <param name="srcX">The x-coordinate of the upper-left corner of the portion of the source image to draw.</param> /// <param name="srcY">The y-coordinate of the upper-left corner of the portion of the source image to draw.</param> /// <param name="srcWidth">Width of the portion of the source image to draw.</param> /// <param name="srcHeight">Height of the portion of the source image to draw.</param> /// <param name="destX">The x-coordinate of the upper-left corner of drawn image.</param> /// <param name="destY">The y-coordinate of the upper-left corner of drawn image.</param> /// <param name="destWidth">The width of the drawn image.</param> /// <param name="destHeight">The height of the drawn image.</param> /// <param name="opacity">The opacity.</param> /// <param name="interpolate">interpolate if set to <c>true</c>.</param> public void DrawImage( OxyImage source, double srcX, double srcY, double srcWidth, double srcHeight, double destX, double destY, double destWidth, double destHeight, double opacity, bool interpolate) { if (destWidth <= 0 || destHeight <= 0 || srcWidth <= 0 || srcHeight <= 0) { return; } var bmp = this.GetBitmap(source); this.renderUnits.Add( new ImageRenderUnit( bmp, new RectangleF((float)srcX, (float)srcY, (float)srcWidth, (float)srcHeight), new RectangleF((float)destX, (float)destY, (float)destWidth, (float)destHeight), (float)opacity, interpolate ? BitmapInterpolationMode.Linear : BitmapInterpolationMode.NearestNeighbor)); }
/// <summary> /// Draws the specified portion of the specified <see cref="OxyImage" /> at the specified location and with the specified size. /// </summary> /// <param name="source">The source.</param> /// <param name="srcX">The x-coordinate of the upper-left corner of the portion of the source image to draw.</param> /// <param name="srcY">The y-coordinate of the upper-left corner of the portion of the source image to draw.</param> /// <param name="srcWidth">Width of the portion of the source image to draw.</param> /// <param name="srcHeight">Height of the portion of the source image to draw.</param> /// <param name="destX">The x-coordinate of the upper-left corner of drawn image.</param> /// <param name="destY">The y-coordinate of the upper-left corner of drawn image.</param> /// <param name="destWidth">The width of the drawn image.</param> /// <param name="destHeight">The height of the drawn image.</param> /// <param name="opacity">The opacity.</param> /// <param name="interpolate">interpolate if set to <c>true</c>.</param> public override void DrawImage( OxyImage source, double srcX, double srcY, double srcWidth, double srcHeight, double destX, double destY, double destWidth, double destHeight, double opacity, bool interpolate) { var image = this.GetImage(source); if (image == null) { return; } var src = new SKRect((int)srcX, (int)srcY, (int)(srcX + srcWidth), (int)(srcY + srcHeight)); var dest = new SKRect(this.Convert(destX), this.Convert(destY), this.Convert(destX + destWidth), this.Convert(destY + destHeight)); this.Reset(); // TODO: support opacity this.canvas.DrawBitmap(image, src, dest, this.paint); }
/// <summary> /// Gets the image from cache or converts the specified <paramref name="source"/> <see cref="OxyImage"/>. /// </summary> /// <param name="source">The source.</param> /// <returns>The image.</returns> private NSImage GetImage(OxyImage source) { if (source == null) { return(null); } if (!this.imagesInUse.Contains(source)) { this.imagesInUse.Add(source); } NSImage src; if (!this.imageCache.TryGetValue(source, out src)) { using (var ms = new System.IO.MemoryStream(source.GetData())) { src = NSImage.FromStream(ms); } if (src != null) { this.imageCache.Add(source, src); } } return(src); }
/// <summary> /// Gets the bitmap source. /// </summary> /// <param name="image">The image.</param> /// <returns>The bitmap source.</returns> private BitmapSource GetImageSource(OxyImage image) { if (image == null) { return(null); } if (!this.imagesInUse.Contains(image)) { this.imagesInUse.Add(image); } BitmapSource src; if (this.imageCache.TryGetValue(image, out src)) { return(src); } using (var ms = new System.IO.MemoryStream(image.GetData())) { var bitmapImage = new BitmapImage(); bitmapImage.SetSource(ms); this.imageCache.Add(image, bitmapImage); return(bitmapImage); } }
/// <summary> /// Gets the bitmap source. /// </summary> /// <param name="image">The image.</param> /// <returns>The bitmap source.</returns> private BitmapSource GetImageSource(OxyImage image) { if (image == null) { return(null); } if (!this.imagesInUse.Contains(image)) { this.imagesInUse.Add(image); } BitmapSource src; if (this.imageCache.TryGetValue(image, out src)) { return(src); } // TODO: improve conversion from byte array to random access stream using (var ms = new MemoryStream(image.GetData())) { var bitmapImage = new BitmapImage(); bitmapImage.SetSource(ConvertToRandomAccessStream(ms).Result); this.imageCache.Add(image, bitmapImage); return(bitmapImage); } }
/// <summary> /// Gets the bitmap source. /// </summary> /// <param name="image">The image.</param> /// <returns>The bitmap source.</returns> private BitmapSource GetImageSource(OxyImage image) { if (image == null) { return(null); } if (!this.imagesInUse.Contains(image)) { this.imagesInUse.Add(image); } BitmapSource src; if (this.imageCache.TryGetValue(image, out src)) { return(src); } var bitmapImage = new BitmapImage(); var imageStream = ConvertToRandomAccessStream(image.GetData()).GetAwaiter().GetResult(); bitmapImage.SetSource(imageStream); this.imageCache.Add(image, bitmapImage); return(bitmapImage); }
/// <summary> /// Gets the image from cache or converts the specified <paramref name="source"/> <see cref="OxyImage"/>. /// </summary> /// <param name="source">The source.</param> /// <returns>The image.</returns> private UIImage GetImage(OxyImage source) { if (source == null) { return(null); } if (!this.imagesInUse.Contains(source)) { this.imagesInUse.Add(source); } UIImage src; if (!this.imageCache.TryGetValue(source, out src)) { using (var data = NSData.FromArray(source.GetData())) { src = UIImage.LoadFromData(data); } this.imageCache.Add(source, src); } return(src); }
/// <summary> /// The download completed, set the image. /// </summary> /// <param name="uri">The URI.</param> /// <param name="result">The result.</param> private void DownloadCompleted(string uri, Stream result) { if (result == null) { return; } var ms = new MemoryStream(); result.CopyTo(ms); var buffer = ms.ToArray(); var img = new OxyImage(buffer); this.images[uri] = img; lock (this.queue) { // Clear old items in the queue, new ones will be added when the plot is refreshed foreach (var queuedUri in this.queue) { // Remove the 'reserved' image this.images.Remove(queuedUri); } this.queue.Clear(); } this.PlotModel.InvalidatePlot(false); if (this.queue.Count > 0) { this.BeginDownload(); } }
/// <summary> /// Draws the image. /// </summary> /// <param name="source">The source.</param> /// <param name="srcX">The source executable.</param> /// <param name="srcY">The source asynchronous.</param> /// <param name="srcWidth">Width of the source.</param> /// <param name="srcHeight">Height of the source.</param> /// <param name="x">The executable.</param> /// <param name="y">The asynchronous.</param> /// <param name="w">The forward.</param> /// <param name="h">The authentication.</param> /// <param name="opacity">The opacity.</param> /// <param name="interpolate">if set to <c>true</c> [interpolate].</param> public override void DrawImage(OxyImage source, double srcX, double srcY, double srcWidth, double srcHeight, double x, double y, double w, double h, double opacity, bool interpolate) { var image = this.GetImage(source); if (image != null) { ImageAttributes ia = null; if (opacity < 1) { var cm = new ColorMatrix { Matrix00 = 1f, Matrix11 = 1f, Matrix22 = 1f, Matrix33 = 1f, Matrix44 = (float)opacity }; ia = new ImageAttributes(); ia.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); } this.g.InterpolationMode = interpolate ? InterpolationMode.HighQualityBicubic : InterpolationMode.NearestNeighbor; int sx = (int)Math.Floor(x); int sy = (int)Math.Floor(y); int sw = (int)Math.Ceiling(x + w) - sx; int sh = (int)Math.Ceiling(y + h) - sy; var destRect = new Rectangle(sx, sy, sw, sh); this.g.DrawImage(image, destRect, (float)srcX - 0.5f, (float)srcY - 0.5f, (float)srcWidth, (float)srcHeight, GraphicsUnit.Pixel, ia); } }
/// <summary> /// Gets bitmap. /// </summary> /// <param name="image">The image.</param> /// <returns>Returns bitmap.</returns> private Bitmap GetBitmap(OxyImage image) { if (image == null) { return(null); } if (!this.imagesInUse.Contains(image)) { this.imagesInUse.Add(image); } Bitmap res; using (var stream = new MemoryStream(image.GetData())) { var decoder = new BitmapDecoder(this.wicFactory, stream, DecodeOptions.CacheOnDemand); var frame = decoder.GetFrame(0); var converter = new FormatConverter(this.wicFactory); converter.Initialize(frame, dx.WIC.PixelFormat.Format32bppPRGBA); res = Bitmap.FromWicBitmap(this.renderTarget, converter); } this.imageCache.Add(image, res); return(res); }
public void FromIndexed8() { var data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 }; var data2 = new byte[, ] { { 0, 1, 2, 3 }, { 4, 5, 6, 7 } }; var palette = new OxyColor[8]; palette[4] = OxyColors.Blue; palette[5] = OxyColors.Green; palette[6] = OxyColors.Red; palette[7] = OxyColors.White; palette[0] = OxyColors.Yellow.ChangeAlpha(127); palette[1] = OxyColors.Orange.ChangeAlpha(127); palette[2] = OxyColors.Pink.ChangeAlpha(127); palette[3] = OxyColors.Transparent; var img = OxyImage.FromIndexed8(4, 2, data, palette); var bytes = img.GetData(); File.WriteAllBytes("FromIndexed8.bmp", bytes); var img2 = OxyImage.FromIndexed8(data2, palette); var bytes2 = img2.GetData(); File.WriteAllBytes("FromIndexed8_2.bmp", bytes2); }
/// <summary> /// Gets the bitmap source. /// </summary> /// <param name="image">The image.</param> /// <returns>The bitmap source.</returns> private IBitmap GetImageSource(OxyImage image) { if (image == null) { return(null); } if (!imagesInUse.Contains(image)) { imagesInUse.Add(image); } IBitmap src; if (imageCache.TryGetValue(image, out src)) { return(src); } using (var ms = new MemoryStream(image.GetData())) { var btm = new Bitmap(ms); imageCache.Add(image, btm); return(btm); } }
/// <summary> /// Gets the image info. /// </summary> /// <param name="source">The source.</param> /// <returns></returns> public override OxyImageInfo GetImageInfo(OxyImage source) { var image = this.GetImage(source); return(image == null ? null : new OxyImageInfo { Width = (uint)image.PixelWidth, Height = (uint)image.PixelHeight, DpiX = image.HorizontalResolution, DpiY = image.VerticalResolution }); }
/// <summary> /// Renders the series on the specified render context. /// </summary> /// <param name="rc">The rendering context.</param> public override void Render(IRenderContext rc) { if (this.Data == null) { this.image = null; return; } if (this.ColorAxis == null) { throw new InvalidOperationException("Color axis not specified."); } double left = this.X0; double right = this.X1; double bottom = this.Y0; double top = this.Y1; int m = this.Data.GetLength(0); int n = this.Data.GetLength(1); double dx = (this.X1 - this.X0) / (m - 1); double dy = (this.Y1 - this.Y0) / (n - 1); if (this.CoordinateDefinition == HeatMapCoordinateDefinition.Center) { left -= dx / 2; right += dx / 2; bottom -= dy / 2; top += dy / 2; } var s00 = this.Transform(left, bottom); var s11 = this.Transform(right, top); var rect = new OxyRect(s00, s11); var currentDataHash = this.Data.GetHashCode(); var currentColorAxisHash = this.ColorAxis.GetElementHashCode(); if (this.image == null || currentDataHash != this.dataHash || currentColorAxisHash != this.colorAxisHash) { this.UpdateImage(); this.dataHash = currentDataHash; this.colorAxisHash = currentColorAxisHash; } var clip = this.GetClippingRect(); if (this.image != null) { rc.DrawClippedImage(clip, this.image, rect.Left, rect.Top, rect.Width, rect.Height, 1, this.Interpolate); } if (this.LabelFontSize > 0) { this.RenderLabels(rc, rect); } }
//// [TestCase("test.jpg", ImageFormat.Jpeg)] public void GetFormat_TestFiles_(string fileName, ImageFormat expectedImageFormat) { var image = new OxyImage(File.ReadAllBytes(@"Imaging\TestImages\" + fileName)); Assert.AreEqual(expectedImageFormat, image.Format); Assert.AreEqual(137, image.Width); Assert.AreEqual(59, image.Height); Assert.AreEqual(72, Math.Round(image.DpiX)); Assert.AreEqual(72, Math.Round(image.DpiY)); }
/// <summary> /// Initializes a new instance of the <see cref="ImageAnnotation"/> class. /// </summary> /// <param name="image"> /// The image. /// </param> /// <param name="position"> /// The position in screen coordinates. /// </param> /// <param name="horizontalAlignment"> /// The horizontal alignment. /// </param> /// <param name="verticalAlignment"> /// The vertical alignment. /// </param> public ImageAnnotation( OxyImage image, ScreenPoint position, HorizontalAlignment horizontalAlignment = OxyPlot.HorizontalAlignment.Center, VerticalAlignment verticalAlignment = OxyPlot.VerticalAlignment.Middle) : this() { this.ImageSource = image; this.X = new PlotLength(position.X, PlotLengthUnit.ScreenUnits); this.Y = new PlotLength(position.Y, PlotLengthUnit.ScreenUnits); this.HorizontalAlignment = horizontalAlignment; this.VerticalAlignment = verticalAlignment; }
/// <summary> /// Draws the specified portion of the specified <see cref="OxyImage" /> at the specified location and with the specified size. /// </summary> /// <param name="source">The source.</param> /// <param name="srcX">The x-coordinate of the upper-left corner of the portion of the source image to draw.</param> /// <param name="srcY">The y-coordinate of the upper-left corner of the portion of the source image to draw.</param> /// <param name="srcWidth">Width of the portion of the source image to draw.</param> /// <param name="srcHeight">Height of the portion of the source image to draw.</param> /// <param name="destX">The x-coordinate of the upper-left corner of drawn image.</param> /// <param name="destY">The y-coordinate of the upper-left corner of drawn image.</param> /// <param name="destWidth">The width of the drawn image.</param> /// <param name="destHeight">The height of the drawn image.</param> /// <param name="opacity">The opacity.</param> /// <param name="interpolate">interpolate if set to <c>true</c>.</param> public override void DrawImage(OxyImage source, double srcX, double srcY, double srcWidth, double srcHeight, double destX, double destY, double destWidth, double destHeight, double opacity, bool interpolate) { var image = this.GetImage(source); if (image != null) { // opacity not suported? // g.InterpolationMode = interpolate ? InterpolationMode.HighQualityBicubic : InterpolationMode.NearestNeighbor; this.g.DrawImage(image, new XRect(destX, destY, destWidth, destHeight), new XRect(srcX, srcY, srcWidth, srcHeight), XGraphicsUnit.Presentation); } }
/// <summary> /// Gets or creates a <see cref="XImage" /> from the specified image. /// </summary> /// <param name="source">The source.</param> /// <returns>The image</returns> private XImage GetImage(OxyImage source) { if (source == null) { return null; } if (!this.imagesInUse.Contains(source)) { this.imagesInUse.Add(source); } XImage src; if (this.imageCache.TryGetValue(source, out src)) { return src; } #if !SILVERLIGHT XImage bitmap; using (var ms = new MemoryStream(source.GetData())) { var im = System.Drawing.Image.FromStream(ms); bitmap = XImage.FromGdiPlusImage(im); } this.imageCache.Add(source, bitmap); return bitmap; #else using (var ms = new MemoryStream(source.GetData())) { var bitmap = XImage.FromStream(ms); this.imageCache.Add(source, bitmap); return bitmap; } #endif }
/// <summary> /// Gets the image from cache or converts the specified <paramref name="source"/> <see cref="OxyImage"/>. /// </summary> /// <param name="source">The source.</param> /// <returns>The image.</returns> private NSImage GetImage(OxyImage source) { if (source == null) { return null; } if (!this.imagesInUse.Contains (source)) { this.imagesInUse.Add (source); } NSImage src; if (!this.imageCache.TryGetValue (source, out src)) { using (var ms = new System.IO.MemoryStream (source.GetData ())) { src = NSImage.FromStream (ms); } if (src != null) { this.imageCache.Add (source, src); } } return src; }
/// <summary> /// Gets the bitmap source. /// </summary> /// <param name="image">The image.</param> /// <returns>The bitmap source.</returns> private BitmapSource GetImageSource(OxyImage image) { if (image == null) { return null; } if (!this.imagesInUse.Contains(image)) { this.imagesInUse.Add(image); } BitmapSource bitmapSource; if (this.imageCache.TryGetValue(image, out bitmapSource)) { return bitmapSource; } var randomAccessStream = new InMemoryRandomAccessStream(); var writer = new DataWriter(randomAccessStream.GetOutputStreamAt(0)); writer.WriteBytes(image.GetData()); writer.StoreAsync(); bitmapSource = new BitmapImage(); bitmapSource.SetSource(randomAccessStream); this.imageCache.Add(image, bitmapSource); return bitmapSource; }
/// <summary> /// Gets the size of the specified image. /// </summary> /// <param name="source">The image source.</param> /// <returns> /// An <see cref="OxyImageInfo" /> structure. /// </returns> public OxyImageInfo GetImageInfo(OxyImage source) { var bmp = this.GetImageSource(source); if (bmp == null) { return null; } return new OxyImageInfo { Width = (uint)bmp.PixelWidth, Height = (uint)bmp.PixelHeight, DpiX = 96, DpiY = 96 }; }
/// <summary> /// Draws the specified portion of the specified <see cref="OxyImage" /> at the specified location and with the specified size. /// </summary> /// <param name="source">The source.</param> /// <param name="srcX">The x-coordinate of the upper-left corner of the portion of the source image to draw.</param> /// <param name="srcY">The y-coordinate of the upper-left corner of the portion of the source image to draw.</param> /// <param name="srcWidth">Width of the portion of the source image to draw.</param> /// <param name="srcHeight">Height of the portion of the source image to draw.</param> /// <param name="destX">The x-coordinate of the upper-left corner of drawn image.</param> /// <param name="destY">The y-coordinate of the upper-left corner of drawn image.</param> /// <param name="destWidth">The width of the drawn image.</param> /// <param name="destHeight">The height of the drawn image.</param> /// <param name="opacity">The opacity.</param> /// <param name="interpolate">interpolate if set to <c>true</c>.</param> public void DrawImage( OxyImage source, double srcX, double srcY, double srcWidth, double srcHeight, double destX, double destY, double destWidth, double destHeight, double opacity, bool interpolate) { if (destWidth <= 0 || destHeight <= 0 || srcWidth <= 0 || srcHeight <= 0) { return; } var image = new Image(); var bmp = this.GetImageSource(source); if (srcX.Equals(0) && srcY.Equals(0) && srcWidth.Equals(bmp.PixelWidth) && srcHeight.Equals(bmp.PixelHeight)) { // do not crop } else { // TODO: cropped image not available in Silverlight?? // bmp = new CroppedBitmap(bmp, new Int32Rect((int)srcX, (int)srcY, (int)srcWidth, (int)srcHeight)); return; } image.Opacity = opacity; image.Width = destWidth; image.Height = destHeight; image.Stretch = Stretch.Fill; // TODO: not available in Silverlight?? // RenderOptions.SetBitmapScalingMode(image, interpolate ? BitmapScalingMode.HighQuality : BitmapScalingMode.NearestNeighbor); // Canvas.SetLeft(image, x); // Canvas.SetTop(image, y); image.RenderTransform = new TranslateTransform { X = destX, Y = destY }; image.Source = bmp; this.ApplyTooltip(image); this.Add(image, destX, destY); }
/// <summary> /// Gets the image from cache or creates a new <see cref="Bitmap" />. /// </summary> /// <param name="source">The source image.</param> /// <returns>A <see cref="Bitmap" />.</returns> private Bitmap GetImage(OxyImage source) { if (source == null) { return null; } if (!this.imagesInUse.Contains(source)) { this.imagesInUse.Add(source); } Bitmap bitmap; if (!this.imageCache.TryGetValue(source, out bitmap)) { var bytes = source.GetData(); bitmap = BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length); this.imageCache.Add(source, bitmap); } return bitmap; }
/// <summary> /// Refreshes the image next time the series is rendered. /// </summary> public void Refresh() { this.image = null; }
/// <summary> /// Renders the series on the specified render context. /// </summary> /// <param name="rc">The rendering context.</param> public override void Render(IRenderContext rc) { if (this.Data == null) { this.image = null; return; } if (this.ImageSize > 0) { this.RenderFixed(rc, this.PlotModel); } else { this.RenderDynamic(rc, this.PlotModel); } }
/// <summary> /// Draws the specified portion of the specified <see cref="OxyImage" /> at the specified location and with the specified size. /// </summary> /// <param name="source">The source.</param> /// <param name="srcX">The x-coordinate of the upper-left corner of the portion of the source image to draw.</param> /// <param name="srcY">The y-coordinate of the upper-left corner of the portion of the source image to draw.</param> /// <param name="srcWidth">Width of the portion of the source image to draw.</param> /// <param name="srcHeight">Height of the portion of the source image to draw.</param> /// <param name="destX">The x-coordinate of the upper-left corner of drawn image.</param> /// <param name="destY">The y-coordinate of the upper-left corner of drawn image.</param> /// <param name="destWidth">The width of the drawn image.</param> /// <param name="destHeight">The height of the drawn image.</param> /// <param name="opacity">The opacity.</param> /// <param name="interpolate">interpolate if set to <c>true</c>.</param> public override void DrawImage( OxyImage source, double srcX, double srcY, double srcWidth, double srcHeight, double destX, double destY, double destWidth, double destHeight, double opacity, bool interpolate) { var image = this.GetImage(source); if (image == null) { return; } var src = new Rect((int)srcX, (int)srcY, (int)(srcX + srcWidth), (int)(srcY + srcHeight)); var dest = new RectF(this.Convert(destX), this.Convert(destY), this.Convert(destX + destWidth), this.Convert(destY + destHeight)); this.paint.Reset(); // TODO: support opacity this.canvas.DrawBitmap(image, src, dest, this.paint); }
/// <summary> /// Gets bitmap. /// </summary> /// <param name="image">The image.</param> /// <returns>Returns bitmap.</returns> private Bitmap GetBitmap(OxyImage image) { if (image == null) { return null; } if (!this.imagesInUse.Contains(image)) { this.imagesInUse.Add(image); } Bitmap res; using (var stream = new MemoryStream(image.GetData())) { var decoder = new BitmapDecoder(this.wicFactory, stream, DecodeOptions.CacheOnDemand); var frame = decoder.GetFrame(0); var converter = new FormatConverter(this.wicFactory); converter.Initialize(frame, dx.WIC.PixelFormat.Format32bppPRGBA); res = Bitmap.FromWicBitmap(this.renderTarget, converter); } this.imageCache.Add(image, res); return res; }
/// <summary> /// Renders by an image sized from the available plot area. /// </summary> /// <param name="rc">The rc.</param> /// <param name="model">The model.</param> public void RenderDynamic(IRenderContext rc, PlotModel model) { int m = this.Data.GetLength(0); int n = this.Data.GetLength(1); // get the available plot area var dest = model.PlotArea; int width = (int)dest.Width; int height = (int)dest.Height; if (width == 0 || height == 0) { return; } if (this.pixels == null || this.pixels.GetLength(0) != height || this.pixels.GetLength(1) != width) { this.pixels = new OxyColor[width, height]; } var p = this.pixels; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // transform from screen to magnitude/angle var sp = new ScreenPoint(dest.Left + x, dest.Top + y); var xy = this.InverseTransform(sp); double angle; double magnitude; if (this.PlotModel.PlotType != PlotType.Polar) { angle = Math.Atan2(xy.Y, xy.X) / Math.PI * 180; magnitude = Math.Sqrt((xy.X * xy.X) + (xy.Y * xy.Y)); } else { angle = xy.Y / Math.PI * 180; magnitude = xy.X; if (angle < 0) { angle += 360; } } // transform to indices in the Data array var ii = (angle - this.Angle0) / (this.Angle1 - this.Angle0) * m; var jj = (magnitude - this.Magnitude0) / (this.Magnitude1 - this.Magnitude0) * n; if (ii >= 0 && ii < m && jj >= 0 && jj < n) { // get the (interpolated) value var value = this.GetValue(ii, jj); // use the color axis to get the color p[x, y] = OxyColor.FromAColor(160, this.ColorAxis.GetColor(value)); } else { // outside the range of the Data array p[x, y] = OxyColors.Transparent; } } } // Create the PNG image this.image = OxyImage.Create(p, ImageFormat.Png); // Render the image var clip = this.GetClippingRect(); rc.DrawClippedImage(clip, this.image, dest.Left, dest.Top, dest.Width, dest.Height, 1, false); }
/// <summary> /// Gets the bitmap source. /// </summary> /// <param name="image">The image.</param> /// <returns>The bitmap source.</returns> private BitmapSource GetImageSource(OxyImage image) { if (image == null) { return null; } if (!this.imagesInUse.Contains(image)) { this.imagesInUse.Add(image); } BitmapSource src; if (this.imageCache.TryGetValue(image, out src)) { return src; } // TODO: improve conversion from byte array to random access stream using (var ms = new MemoryStream(image.GetData())) { var bitmapImage = new BitmapImage(); bitmapImage.SetSource(ConvertToRandomAccessStream(ms).Result); this.imageCache.Add(image, bitmapImage); return bitmapImage; } }
/// <summary> /// Draws the specified portion of the specified <see cref="OxyImage" /> at the specified location and with the specified size. /// </summary> /// <param name="source">The source.</param> /// <param name="srcX">The x-coordinate of the upper-left corner of the portion of the source image to draw.</param> /// <param name="srcY">The y-coordinate of the upper-left corner of the portion of the source image to draw.</param> /// <param name="srcWidth">Width of the portion of the source image to draw.</param> /// <param name="srcHeight">Height of the portion of the source image to draw.</param> /// <param name="destX">The x-coordinate of the upper-left corner of drawn image.</param> /// <param name="destY">The y-coordinate of the upper-left corner of drawn image.</param> /// <param name="destWidth">The width of the drawn image.</param> /// <param name="destHeight">The height of the drawn image.</param> /// <param name="opacity">The opacity.</param> /// <param name="interpolate">interpolate if set to <c>true</c>.</param> public void DrawImage( OxyImage source, uint srcX, uint srcY, uint srcWidth, uint srcHeight, double destX, double destY, double destWidth, double destHeight, double opacity, bool interpolate) { // TODO }
/// <summary> /// Initializes a new instance of the <see cref="ImageAnnotation"/> class. /// </summary> /// <param name="image"> /// The image. /// </param> /// <param name="relativeX"> /// The x-coordinate relative to the plot area (0-1). /// </param> /// <param name="relativeY"> /// The y-coordinate relative to the plot area (0-1). /// </param> /// <param name="horizontalAlignment"> /// The horizontal alignment. /// </param> /// <param name="verticalAlignment"> /// The vertical alignment. /// </param> public ImageAnnotation( OxyImage image, double relativeX, double relativeY, HorizontalAlignment horizontalAlignment = OxyPlot.HorizontalAlignment.Center, VerticalAlignment verticalAlignment = OxyPlot.VerticalAlignment.Middle) : this() { this.ImageSource = image; this.X = new PlotLength(relativeX, PlotLengthUnit.RelativeToPlotArea); this.Y = new PlotLength(relativeY, PlotLengthUnit.RelativeToPlotArea); this.HorizontalAlignment = horizontalAlignment; this.VerticalAlignment = verticalAlignment; }
/// <summary> /// Downloads the image from the specified URI. /// </summary> /// <param name="uri">The URI.</param> /// <returns>The image</returns> private OxyImage Download(string uri) { OxyImage img = null; var mre = new ManualResetEvent(false); var request = (HttpWebRequest)WebRequest.Create(uri); request.Method = "GET"; request.BeginGetResponse( r => { try { if (request.HaveResponse) { var response = request.EndGetResponse(r); var stream = response.GetResponseStream(); var ms = new MemoryStream(); stream.CopyTo(ms); var buffer = ms.ToArray(); img = new OxyImage(buffer); this.images[uri] = img; } } catch (Exception e) { var ie = e; while (ie != null) { System.Diagnostics.Debug.WriteLine(ie.Message); ie = ie.InnerException; } } finally { mre.Set(); } }, request); mre.WaitOne(); return img; }
/// <summary> /// Updates the image. /// </summary> private void UpdateImage() { // determine if the provided data should be reversed in x-direction var reverseX = this.XAxis.Transform(this.X0) > this.XAxis.Transform(this.X1); // determine if the provided data should be reversed in y-direction var reverseY = this.YAxis.Transform(this.Y0) > this.YAxis.Transform(this.Y1); int m = this.Data.GetLength(0); int n = this.Data.GetLength(1); var buffer = new OxyColor[m, n]; for (int i = 0; i < m; i++) { var ii = reverseX ? m - 1 - i : i; for (int j = 0; j < n; j++) { var jj = reverseY ? n - 1 - j : j; buffer[i, j] = this.ColorAxis.GetColor(this.Data[ii, jj]); } } this.image = OxyImage.Create(buffer, ImageFormat.Png); }
/// <summary> /// Gets the bitmap source. /// </summary> /// <param name="image">The image.</param> /// <returns>The bitmap source.</returns> private BitmapSource GetImageSource(OxyImage image) { if (image == null) { return null; } if (!this.imagesInUse.Contains(image)) { this.imagesInUse.Add(image); } BitmapSource src; if (this.imageCache.TryGetValue(image, out src)) { return src; } using (var ms = new System.IO.MemoryStream(image.GetData())) { var bitmapImage = new BitmapImage(); bitmapImage.SetSource(ms); this.imageCache.Add(image, bitmapImage); return bitmapImage; } }
/// <summary> /// Invalidates the image that renders the heat map. The image will be regenerated the next time the <see cref="HeatMapSeries" /> is rendered. /// </summary> /// <remarks>Call <see cref="PlotModel.InvalidatePlot" /> to refresh the view.</remarks> public void Invalidate() { this.image = null; }
/// <summary> /// Draws the specified portion of the specified <see cref="OxyImage"/> at the specified location and with the specified size. /// </summary> /// <param name="source">The source.</param> /// <param name="srcX">The x-coordinate of the upper-left corner of the portion of the source image to draw.</param> /// <param name="srcY">The y-coordinate of the upper-left corner of the portion of the source image to draw.</param> /// <param name="srcWidth">Width of the portion of the source image to draw.</param> /// <param name="srcHeight">Height of the portion of the source image to draw.</param> /// <param name="destX">The x-coordinate of the upper-left corner of drawn image.</param> /// <param name="destY">The y-coordinate of the upper-left corner of drawn image.</param> /// <param name="destWidth">The width of the drawn image.</param> /// <param name="destHeight">The height of the drawn image.</param> /// <param name="opacity">The opacity.</param> /// <param name="interpolate">interpolate if set to <c>true</c>.</param> public void DrawImage( OxyImage source, uint srcX, uint srcY, uint srcWidth, uint srcHeight, double destX, double destY, double destWidth, double destHeight, double opacity, bool interpolate) { if (destWidth <= 0 || destHeight <= 0 || srcWidth <= 0 || srcHeight <= 0) { return; } var image = new Image(); var bitmapChain = this.GetImageSource(source); if (srcX == 0 && srcY == 0 && srcWidth == bitmapChain.PixelWidth && srcHeight == bitmapChain.PixelHeight) { // do not crop } else { throw new NotSupportedException("Use DrawClippedImage, CroppedBitmap is not supported here."); } image.Opacity = opacity; image.Width = destWidth; image.Height = destHeight; image.Stretch = Stretch.Fill; //// RenderOptions.SetBitmapScalingMode(image, interpolate ? BitmapScalingMode.HighQuality : BitmapScalingMode.NearestNeighbor); // Set the position of the image Canvas.SetLeft(image, destX); Canvas.SetTop(image, destY); //// alternative: image.RenderTransform = new TranslateTransform(destX, destY); //// TODO: check performance? image.Source = bitmapChain; this.Add(image, destX, destY); }
/// <summary> /// Gets the image from cache or converts the specified <paramref name="source"/> <see cref="OxyImage"/>. /// </summary> /// <param name="source">The source.</param> /// <returns>The image.</returns> private UIImage GetImage(OxyImage source) { if (source == null) { return null; } if (!this.imagesInUse.Contains(source)) { this.imagesInUse.Add(source); } UIImage src; if (!this.imageCache.TryGetValue(source, out src)) { using (var data = NSData.FromArray(source.GetData())) { src = UIImage.LoadFromData(data); } this.imageCache.Add(source, src); } return src; }
/// <summary> /// Renders the series on the specified render context. /// </summary> /// <param name="rc">The rendering context.</param> /// <param name="model">The model.</param> public override void Render(IRenderContext rc, PlotModel model) { if (this.Data == null) { this.image = null; return; } double left = this.X0; double right = this.X1; double bottom = this.Y0; double top = this.Y1; int m = this.Data.GetLength(0); int n = this.Data.GetLength(1); double dx = (this.X1 - this.X0) / (m - 1); double dy = (this.Y1 - this.Y0) / (n - 1); if (this.CoordinateDefinition == HeatMapCoordinateDefinition.Center) { left -= dx / 2; right += dx / 2; bottom -= dy / 2; top += dy / 2; } var s00 = this.Transform(left, bottom); var s11 = this.Transform(right, top); var rect = new OxyRect(s00, s11); var currentDataHash = this.Data.GetHashCode(); var currentColorAxisHash = this.ColorAxis.GetElementHashCode(); if (this.image == null || currentDataHash != this.dataHash || currentColorAxisHash != this.colorAxisHash) { this.UpdateImage(); this.dataHash = currentDataHash; this.colorAxisHash = currentColorAxisHash; } var clip = this.GetClippingRect(); if (this.image != null) { rc.DrawClippedImage(clip, this.image, rect.Left, rect.Top, rect.Width, rect.Height, 1, this.Interpolate); } if (this.LabelFontSize > 0) { this.RenderLabels(rc, rect); } }
/// <summary> /// Draws the specified portion of the specified <see cref="OxyImage" /> at the specified location and with the specified size. /// </summary> /// <param name="source">The source.</param> /// <param name="srcX">The x-coordinate of the upper-left corner of the portion of the source image to draw.</param> /// <param name="srcY">The y-coordinate of the upper-left corner of the portion of the source image to draw.</param> /// <param name="srcWidth">Width of the portion of the source image to draw.</param> /// <param name="srcHeight">Height of the portion of the source image to draw.</param> /// <param name="destX">The x-coordinate of the upper-left corner of drawn image.</param> /// <param name="destY">The y-coordinate of the upper-left corner of drawn image.</param> /// <param name="destWidth">The width of the drawn image.</param> /// <param name="destHeight">The height of the drawn image.</param> /// <param name="opacity">The opacity.</param> /// <param name="interpolate">Interpolate if set to <c>true</c>.</param> public override void DrawImage(OxyImage source, double srcX, double srcY, double srcWidth, double srcHeight, double destX, double destY, double destWidth, double destHeight, double opacity, bool interpolate) { var image = this.GetImage (source); if (image == null) { return; } this.gctx.SaveState (); double x = destX - (srcX / srcWidth * destWidth); double y = destY - (srcY / srcHeight * destHeight); this.gctx.ScaleCTM (1, -1); this.gctx.TranslateCTM ((float)x, -(float)(y + destHeight)); this.gctx.SetAlpha ((float)opacity); this.gctx.InterpolationQuality = interpolate ? CGInterpolationQuality.High : CGInterpolationQuality.None; var destRect = new RectangleF (0f, 0f, (float)destWidth, (float)destHeight); this.gctx.DrawImage (destRect, image.CGImage); this.gctx.RestoreState (); }
/// <summary> /// Gets the size of the specified image. /// </summary> /// <param name="source">The image source.</param> /// <returns> /// The image info. /// </returns> public OxyImageInfo GetImageInfo(OxyImage source) { // TODO return null; }
/// <summary> /// Renders by scaling a fixed image. /// </summary> /// <param name="rc">The render context.</param> /// <param name="model">The model.</param> public void RenderFixed(IRenderContext rc, PlotModel model) { if (image == null) { int m = this.Data.GetLength(0); int n = this.Data.GetLength(1); int width = this.ImageSize; int height = this.ImageSize; if (this.pixels == null || this.pixels.GetLength(0) != height || this.pixels.GetLength(1) != width) { this.pixels = new OxyColor[width, height]; } var p = this.pixels; for (int yi = 0; yi < height; yi++) { for (int xi = 0; xi < width; xi++) { double x = (xi - width * 0.5) / (width * 0.5) * this.Magnitude1; double y = -(yi - height * 0.5) / (height * 0.5) * this.Magnitude1; double angle = Math.Atan2(y, x) / Math.PI * 180; double magnitude = Math.Sqrt(x * x + y * y); if (angle < 0) { angle += 360; } // transform to indices in the Data array var ii = (angle - this.Angle0) / (this.Angle1 - this.Angle0) * m; var jj = (magnitude - this.Magnitude0) / (this.Magnitude1 - this.Magnitude0) * n; if (ii >= 0 && ii < m && jj >= 0 && jj < n) { // get the (interpolated) value var value = this.GetValue(ii, jj); // use the color axis to get the color p[xi, yi] = OxyColor.FromAColor(160, this.ColorAxis.GetColor(value)); } else { // outside the range of the Data array p[xi, yi] = OxyColors.Transparent; } } } // Create the PNG image this.image = OxyImage.Create(p, ImageFormat.Png); } OxyRect dest; if (this.PlotModel.PlotType != PlotType.Polar) { var topleft = this.Transform(-this.Magnitude1, this.Magnitude1); var bottomright = this.Transform(this.Magnitude1, -this.Magnitude1); dest = new OxyRect(topleft.X, topleft.Y, bottomright.X - topleft.X, bottomright.Y - topleft.Y); } else { var top = this.Transform(this.Magnitude1, 90); var bottom = this.Transform(this.Magnitude1, 270); var left = this.Transform(this.Magnitude1, 180); var right = this.Transform(this.Magnitude1, 0); dest = new OxyRect(left.X, top.Y, right.X - left.X, bottom.Y - top.Y); } // Render the image var clip = this.GetClippingRect(); rc.DrawClippedImage(clip, this.image, dest.Left, dest.Top, dest.Width, dest.Height, 1, false); }