Search Results

Search found 2 results on 1 pages for 'moozhe'.

Page 1/1 | 1 

  • Creating a GraphicsPath from a semi-transparent bitmap

    - by Moozhe
    I want to create a GraphicsPath and a list of Points to form the outline of the non-transparent area of a bitmap. If needed, I can guarantee that each image has only one solid collection of nontransparent pixels. So for example, I should be able to record the points either clockwise or counter-clockwise along the edge of the pixels and perform a full closed loop. The speed of this algorithm is not important. However, the efficiency of the resulting points is semi-important if I can skip some points to reduce in a smaller and less complex GraphicsPath. I will list my current code below which works perfectly with most images. However, some images which are more complex end up with paths which seem to connect in the wrong order. I think I know why this occurs, but I can't come up with a solution. public static Point[] GetOutlinePoints(Bitmap image) { List<Point> outlinePoints = new List<Point>(); BitmapData bitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); byte[] originalBytes = new byte[image.Width * image.Height * 4]; Marshal.Copy(bitmapData.Scan0, originalBytes, 0, originalBytes.Length); for (int x = 0; x < bitmapData.Width; x++) { for (int y = 0; y < bitmapData.Height; y++) { byte alpha = originalBytes[y * bitmapData.Stride + 4 * x + 3]; if (alpha != 0) { Point p = new Point(x, y); if (!ContainsPoint(outlinePoints, p)) outlinePoints.Add(p); break; } } } for (int y = 0; y < bitmapData.Height; y++) { for (int x = bitmapData.Width - 1; x >= 0; x--) { byte alpha = originalBytes[y * bitmapData.Stride + 4 * x + 3]; if (alpha != 0) { Point p = new Point(x, y); if (!ContainsPoint(outlinePoints, p)) outlinePoints.Add(p); break; } } } for (int x = bitmapData.Width - 1; x >= 0; x--) { for (int y = bitmapData.Height - 1; y >= 0; y--) { byte alpha = originalBytes[y * bitmapData.Stride + 4 * x + 3]; if (alpha != 0) { Point p = new Point(x, y); if (!ContainsPoint(outlinePoints, p)) outlinePoints.Add(p); break; } } } for (int y = bitmapData.Height - 1; y >= 0; y--) { for (int x = 0; x < bitmapData.Width; x++) { byte alpha = originalBytes[y * bitmapData.Stride + 4 * x + 3]; if (alpha != 0) { Point p = new Point(x, y); if (!ContainsPoint(outlinePoints, p)) outlinePoints.Add(p); break; } } } // Added to close the loop outlinePoints.Add(outlinePoints[0]); image.UnlockBits(bitmapData); return outlinePoints.ToArray(); } public static bool ContainsPoint(IEnumerable<Point> points, Point value) { foreach (Point p in points) { if (p == value) return true; } return false; } And when I turn the points into a path: GraphicsPath outlinePath = new GraphicsPath(); outlinePath.AddLines(_outlinePoints); Here's an example showing what I want. The red outline should be an array of points which can be made into a GraphicsPath in order to perform hit detection, draw an outline pen, and fill it with a brush.

    Read the article

  • Running multiple image manipulations in parallel causing OutOfMemory exception

    - by Tom
    I am working on a site where I need to be able to split and image around 4000x6000 into 4 parts (amongst many other tasks) and I need this to be as quick as possible for multiple users. My current code for doing this is var bitmaps = new RenderTargetBitmap[elements.Length]; using (var stream = blobService.Stream(key)) { BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = stream; bi.EndInit(); for (var i = 0; i < elements.Length; i++) { var element = elements[i]; TransformGroup transformGroup = new TransformGroup(); TranslateTransform translateTransform = new TranslateTransform(); translateTransform.X = -element.Left; translateTransform.Y = -element.Top; transformGroup.Children.Add(translateTransform); DrawingVisual vis = new DrawingVisual(); DrawingContext cont = vis.RenderOpen(); cont.PushTransform(transformGroup); cont.DrawImage(bi, new Rect(new Size(bi.PixelWidth, bi.PixelHeight))); cont.Close(); RenderTargetBitmap rtb = new RenderTargetBitmap(element.Width, element.Height, 96d, 96d, PixelFormats.Default); rtb.Render(vis); bitmaps[i] = rtb; } } for (var i = 0; i < bitmaps.Length; i++) { using (MemoryStream ms = new MemoryStream()) { PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmaps[i])); encoder.Save(ms); var regionKey = WebPath.Variant(key, elements[i].Id); saveBlobService.Save("image/png", regionKey, ms); } } I am running multiple threads which take jobs off a queue. I am finding that if this part of code is hit by 4 threads at once I get an OutOfMemory exception. I can stop this happening by wrapping all the code above in a lock(obj) but this isn't ideal. I have tried wrapping just the first using block (where the file is read from disk and split) but I still get the out of memory exceptions (this part of the code executes quite quickly). I this normal considering the amount of memory this should be taking up? Are there any optimisations I could make? Can I increase the memory available? UPDATE: My new code as per Moozhe's help public static void GenerateRegions(this IBlobService blobService, string key, Element[] elements) { using (var stream = blobService.Stream(key)) { foreach (var element in elements) { stream.Position = 0; BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.SourceRect = new Int32Rect(element.Left, element.Top, element.Width, element.Height); bi.StreamSource = stream; bi.EndInit(); DrawingVisual vis = new DrawingVisual(); DrawingContext cont = vis.RenderOpen(); cont.DrawImage(bi, new Rect(new Size(element.Width, element.Height))); cont.Close(); RenderTargetBitmap rtb = new RenderTargetBitmap(element.Width, element.Height, 96d, 96d, PixelFormats.Default); rtb.Render(vis); using (MemoryStream ms = new MemoryStream()) { PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(rtb)); encoder.Save(ms); var regionKey = WebPath.Variant(key, element.Id); blobService.Save("image/png", regionKey, ms); } } } }

    Read the article

1