Welcome Guest! You need to login or register to make posts.

Notification

Icon
Error

Options
Go to last post Go to first unread
wony  
#1 Posted : Tuesday, November 25, 2014 7:33:10 AM(UTC)
wony

Rank: Newbie

Groups: Member
Joined: 11/25/2014(UTC)
Posts: 6

Hello.

I am trying to upgrade my current version 5 to the latest 7, and my Visual Studio appears to be giving problems (no definition found) with the highlighted part of the code below.


Quote:
public static Bitmap CropImage(Bitmap objBitmap)
{
Color edgeColor = objBitmap.GetPixel(0, 0);
byte[] colorData = edgeColor.ToArray();

int top = 0;
int bottom = objBitmap.Height - 1;
int left = 0;
int right = objBitmap.Width - 1;

int pixelSize = objBitmap.PixelFormat.Size / 8;
BitmapData bitmapData = objBitmap.LockBits();
.
.
.
objBitmap.UnlockBits(bitmapData);
.
.
.

return objBitmap;
}



This code works fine as version 5.



Thanks,
Won
Fedor  
#2 Posted : Tuesday, November 25, 2014 8:42:03 AM(UTC)
Fedor

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 7/28/2003(UTC)
Posts: 1,660

Thanks: 5 times
Was thanked: 76 time(s) in 74 post(s)
Hi Won,

We have eliminated the structure BitmapData as well as the methods LockBits and UnlockBits. The properties of BitmapData are available via the class Bitmap:

Bitmap.Scan0
Bitmap.Stride

So you should either rewrite the code with the new API or implement a set of extension methods which emulates the old API. For example you can use the following code to create the extension method ToArray:

Code:
using System;
using Aurigma.GraphicsMill;

namespace Aurigma.GraphicsMill
{
	public static class GraphicsMill5Extensions
	{
		public static byte[] ToArray(this Color color)
		{
			var rgbColor = color as RgbColor;
			if (rgbColor != null)
			{
				return new byte[] {rgbColor.B, rgbColor.G, rgbColor.R, rgbColor.A};
			}

			var cmykColor = color as CmykColor;
			if (cmykColor != null)
			{
				return new byte[] { cmykColor.K, cmykColor.Y, cmykColor.M, cmykColor.C, cmykColor.A };
			}

			var grayscaleColor = color as GrayscaleColor;
			if (grayscaleColor != null)
			{
				return new byte[] { grayscaleColor.L, grayscaleColor.A};
			}

			throw new Exception("The color space is not supported.");
		}
	}
}

namespace ColorTest
{
	class Program
	{
		static void Main(string[] args)
		{
			var color = RgbColor.Red;

			byte[] array = color.ToArray();
		}
	}
}


If you need help with the extension methods please post or submit a case with the entire code. It will help us to understand the usage of the old API.

Edited by user Tuesday, November 25, 2014 8:42:53 AM(UTC)  | Reason: Not specified

Best regards,
Fedor Skvortsov
Fedor  
#3 Posted : Tuesday, November 25, 2014 8:44:20 AM(UTC)
Fedor

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 7/28/2003(UTC)
Posts: 1,660

Thanks: 5 times
Was thanked: 76 time(s) in 74 post(s)
BTW, you find the list of changes in the API here:

http://www.graphicsmill....from-graphics-mill-5.htm
Best regards,
Fedor Skvortsov
wony  
#4 Posted : Tuesday, November 25, 2014 10:14:41 AM(UTC)
wony

Rank: Newbie

Groups: Member
Joined: 11/25/2014(UTC)
Posts: 6

Hi Fedor,

Below is the class where I need the change:

Code:

using System;
using System.Collections.Generic;
using System.Text;
using Aurigma.GraphicsMill;
using System.Runtime.InteropServices;

namespace CropImage
{
    public class CropImage
    {

        [DllImport("msvcrt.dll")]
        private static extern unsafe int memcmp(byte* b1, byte* b2, int count);
        static int _delta = 1;

        public static Bitmap CroopImage(Bitmap objBitmap)
        {
            Color edgeColor = objBitmap.GetPixel(0, 0);
            byte[] colorData = edgeColor.ToArray();

            int top = 0;
            int bottom = objBitmap.Height - 1;
            int left = 0;
            int right = objBitmap.Width - 1;

            int pixelSize = objBitmap.BitsPerPixel / 8;

            BitmapData bitmapData = objBitmap.LockBits();

            unsafe
            {
                byte* pScan0 = (byte*)bitmapData.Scan0;

                while (CheckScanline(pScan0 + bitmapData.Stride * top, objBitmap.Width, colorData, pixelSize) && top < objBitmap.Height)
                {
                    top++;
                }

                while (CheckScanline(pScan0 + bitmapData.Stride * bottom, objBitmap.Width, colorData, pixelSize) && top < bottom)
                {
                    bottom--;
                }

                while (CheckRow(pScan0 + bitmapData.Stride * top + left * pixelSize, bottom - top, bitmapData.Stride, colorData, pixelSize) && left < objBitmap.Width)
                {
                    left++;
                }

                while (CheckRow(pScan0 + bitmapData.Stride * top + right * pixelSize, bottom - top, bitmapData.Stride, colorData, pixelSize) && left < right)
                {
                    right--;
                }
            }

            objBitmap.UnlockBits(bitmapData);

            System.Drawing.Rectangle cropRect = new System.Drawing.Rectangle(left, top, right - left, bottom - top);

            objBitmap.Transforms.Crop(cropRect);

            return objBitmap;
        }

        private static unsafe bool CheckScanline(byte* pScanline, int width, byte[] pixel, int pixelSize)
        {
            fixed (byte* pPixelBytes = pixel)
            {
                for (int i = 0; i < width; i++)
                {
                    for (int k = 0; k < pixelSize; k++)
                    {
                        if (Math.Abs(pPixelBytes[k] - pScanline[k]) > _delta)
                            return false;
                    }

                    pScanline += pixelSize;
                }
            }

            return true;
        }

        private static unsafe bool CheckRow(byte* pTop, int height, int stride, byte[] pixel, int pixelSize)
        {
            fixed (byte* pPixelBytes = pixel)
            {
                for (int i = 0; i < height; i++)
                {
                    for (int k = 0; k < pixelSize; k++)
                    {
                        if (Math.Abs(pPixelBytes[k] - pTop[k]) > _delta)
                            return false;
                    }

                    pTop += stride;
                }
            }

            return true;
        }
    
    }
}


wony  
#5 Posted : Tuesday, November 25, 2014 10:58:03 AM(UTC)
wony

Rank: Newbie

Groups: Member
Joined: 11/25/2014(UTC)
Posts: 6

Also, I don't see any mentioning about BitmapData,.LockBits() and .UnlockBits() in the list of changes. Can you tell me what would be the equivalent syntax for them?
Fedor  
#6 Posted : Tuesday, November 25, 2014 5:33:43 PM(UTC)
Fedor

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 7/28/2003(UTC)
Posts: 1,660

Thanks: 5 times
Was thanked: 76 time(s) in 74 post(s)
There is no need to use the methods LockBits and UnlockBits at all. You should synchronize the access to the instance of Bitmap on the application level.

Here is the modified code:

Code:
using System;
using Aurigma.GraphicsMill;
using System.Runtime.InteropServices;

namespace Aurigma.GraphicsMill
{
	public static class GraphicsMill5Extensions
	{
		public static byte[] ToArray(this Color color)
		{
			var rgbColor = color as RgbColor;
			if (rgbColor != null)
			{
				return new byte[] {rgbColor.B, rgbColor.G, rgbColor.R, rgbColor.A};
			}

			var cmykColor = color as CmykColor;
			if (cmykColor != null)
			{
				return new byte[] { cmykColor.K, cmykColor.Y, cmykColor.M, cmykColor.C, cmykColor.A };
			}

			var grayscaleColor = color as GrayscaleColor;
			if (grayscaleColor != null)
			{
				return new byte[] { grayscaleColor.L, grayscaleColor.A};
			}

			throw new Exception("The color space is not supported.");
		}
	}
}

namespace CropImage
{
	public class CropImage
	{

		[DllImport("msvcrt.dll")]
		private static extern unsafe int memcmp(byte* b1, byte* b2, int count);
		static int _delta = 1;

		public static Bitmap CroopImage(Bitmap objBitmap)
		{
			Color edgeColor = objBitmap.GetPixel(0, 0);
			byte[] colorData = edgeColor.ToArray();

			int top = 0;
			int bottom = objBitmap.Height - 1;
			int left = 0;
			int right = objBitmap.Width - 1;

			int pixelSize = objBitmap.PixelFormat.Size / 8;

			unsafe
			{
				byte* pScan0 = (byte*)objBitmap.Scan0;

				while (CheckScanline(pScan0 + objBitmap.Stride * top, objBitmap.Width, colorData, pixelSize) && top < objBitmap.Height)
				{
					top++;
				}

				while (CheckScanline(pScan0 + objBitmap.Stride * bottom, objBitmap.Width, colorData, pixelSize) && top < bottom)
				{
					bottom--;
				}

				while (CheckRow(pScan0 + objBitmap.Stride * top + left * pixelSize, bottom - top, objBitmap.Stride, colorData, pixelSize) && left < objBitmap.Width)
				{
					left++;
				}

				while (CheckRow(pScan0 + objBitmap.Stride * top + right * pixelSize, bottom - top, objBitmap.Stride, colorData, pixelSize) && left < right)
				{
					right--;
				}
			}

			System.Drawing.Rectangle cropRect = new System.Drawing.Rectangle(left, top, right - left, bottom - top);

			//Actually we return the same instance of Bitmap as passed to the method
			objBitmap.Transforms.Crop(cropRect);

			return objBitmap;
		}

		private static unsafe bool CheckScanline(byte* pScanline, int width, byte[] pixel, int pixelSize)
		{
			fixed (byte* pPixelBytes = pixel)
			{
				for (int i = 0; i < width; i++)
				{
					for (int k = 0; k < pixelSize; k++)
					{
						if (Math.Abs(pPixelBytes[k] - pScanline[k]) > _delta)
							return false;
					}

					pScanline += pixelSize;
				}
			}

			return true;
		}

		private static unsafe bool CheckRow(byte* pTop, int height, int stride, byte[] pixel, int pixelSize)
		{
			fixed (byte* pPixelBytes = pixel)
			{
				for (int i = 0; i < height; i++)
				{
					for (int k = 0; k < pixelSize; k++)
					{
						if (Math.Abs(pPixelBytes[k] - pTop[k]) > _delta)
							return false;
					}

					pTop += stride;
				}
			}

			return true;
		}

	}
}

namespace CropTest
{
	class Program
	{
		static void Main(string[] args)
		{
			using (var bitmap = new Bitmap(@"C:\temp\crop.png"))
			{
				var cropped = CropImage.CropImage.CroopImage(bitmap);

				cropped.Save(@"c:\temp\cropped.png");
			}
		}
	}
}


BTW, Graphics Mill 7 has the built-in method for the automatic crop:

Code:
using (var bitmap = new Bitmap(@"C:\temp\crop.png"))
{
	bitmap.Transforms.AutoCrop(bitmap.GetPixel(0, 0));

	bitmap.Save(@"c:\temp\cropped2.png");
}
Best regards,
Fedor Skvortsov
wony  
#7 Posted : Monday, December 1, 2014 7:59:38 AM(UTC)
wony

Rank: Newbie

Groups: Member
Joined: 11/25/2014(UTC)
Posts: 6

Hi Fedor,

Thanks so much for your help!

I was able to get the code working by using the built in crop method.

I am having another issue with the code below. I've highlighted the parts where Visual Studio is complaining.

Code:


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Drawing;
using Aurigma.GraphicsMill.AjaxControls.VectorObjects;
using System.Collections.Generic;
using System.Text;

public partial class PhotoLabelControl : System.Web.UI.UserControl, IScriptControl
{
    public class RegionStyle
    {
        private float _borderWidth = 1;

        public float BorderWidth
        {
            get { return _borderWidth; }
            set { _borderWidth = value; }
        }
        private Color _borderColor = System.Drawing.Color.Black;

        public Color BorderColor
        {
            get { return _borderColor; }
            set { _borderColor = value; }
        }
        private Color _fillColor = System.Drawing.Color.FromArgb((int)0x2054FFCB);

        public Color FillColor
        {
            get { return _fillColor; }
            set { _fillColor = value; }
        }

        public string GetJSON()
        {
            StringBuilder sb = new StringBuilder(100);

            sb.Append("{ ");

            sb.Append("BorderWidth: ");
            sb.Append(this.BorderWidth);
            sb.Append(", BorderColor: \"");
            sb.Append(Common.ConvertToWebColor(this.BorderColor));
            sb.Append("\", FillColor: \"");
            sb.Append(Common.ConvertToWebColor(this.FillColor));
            sb.Append("\" }");

            return sb.ToString();
        }
    }

    private Layer _photoLayer;
    private Layer _regionLayer;
    private const string _photoLayerName = "__backgroundImageLayer";
    private const string _regionLayerName = "__regionRectanglesLayer";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            LoadPhoto();
        }
        else
        {
            _photoLayer = _canvasViewer.Canvas.Layers.GetLayersByName(_photoLayerName)[0];
            _regionLayer = _canvasViewer.Canvas.Layers.GetLayersByName(_regionLayerName)[0];
        }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        if (!IsPostBack)
        {
            Clear();
        }
    }

    public void Clear()
    {
        _canvasViewer.Canvas.Clear();
        _photoLayer = new Layer();
        _photoLayer.Name = _photoLayerName;
        _photoLayer.Locked = true;
        _canvasViewer.Canvas.Layers.Add(_photoLayer);

        _regionLayer = new Layer();
        _regionLayer.Name = _regionLayerName;
        _regionLayer.Locked = true;
        _canvasViewer.Canvas.Layers.Add(_regionLayer);
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        if (!this.DesignMode)
        {
            // Test for ScriptManager and register if it exists
            ScriptManager sm = ScriptManager.GetCurrent(this.Page);

            if (sm == null)
                throw new HttpException("A ScriptManager control must exist on the current page.");

            sm.RegisterScriptControl(this);
        }
    }

    protected override void Render(HtmlTextWriter writer)
    {
        if (!this.DesignMode)
            ScriptManager.GetCurrent(this.Page).RegisterScriptDescriptors(this);

        base.Render(writer);
    }

    public override string ClientID
    {
        get
        {
            return container.ClientID;
        }
    }

    private void LoadPhoto()
    {
        _photoLayer.VObjects.Clear();

        if (!string.IsNullOrEmpty(this.BackgroundImage))
        {
            if (File.Exists(this.BackgroundImage))
            {
                Size imageSize;
                float hRes, vRes;
                Common.GetImageSize(this.BackgroundImage, out imageSize, out hRes, out vRes);
                _canvasViewer.Canvas.WorkspaceHeight = Common.ConvertPixelsToPoints(vRes, imageSize.Height);
                _canvasViewer.Canvas.WorkspaceWidth = Common.ConvertPixelsToPoints(hRes, imageSize.Width);

                //we change canvas size, so we need update zoom to save ZoomMode
                _canvasViewer.ZoomMode = _canvasViewer.ZoomMode;

                ImageVObject vo = new ImageVObject(new FileInfo(this.BackgroundImage));
                vo.BorderWidth = 0;
                vo.Locked = true;
                Aurigma.GraphicsMill.AjaxControls.VectorObjects.Math.RotatedRectangleF r = vo.Rectangle;
                r.Width = _canvasViewer.Canvas.WorkspaceWidth;
                r.Height = _canvasViewer.Canvas.WorkspaceHeight;
                r.Location = new Aurigma.GraphicsMill.AjaxControls.VectorObjects.Math.PointF(0, 0);
                vo.Rectangle = r;
                _photoLayer.VObjects.Insert(0, vo);
            }
            else
            {
                throw new FileNotFoundException("Image file not found.", this.BackgroundImage);
            }
        }
    }

    #region Public Properties

    public string BackgroundImage
    {
        get { return (string)this.ViewState["BackgroundImage"]; }
        set
        {
            this.ViewState["BackgroundImage"] = value;
            LoadPhoto();
        }
    }

    /// <summary>
    /// Whether or not draw rectangle on region places
    /// </summary>
    public bool IsHighlightRegion
    {
        get
        {
            if (this.ViewState["HighlightRegion"] != null)
                return (bool)this.ViewState["HighlightRegion"];
            else
                return false;
        }
        set { this.ViewState["HighlightRegion"] = value; }
    }

    [PersistenceModeAttribute(PersistenceMode.InnerProperty)]
    public RegionStyle CurrentRegionDisplayStyle
    {
        get
        {
            if (this.ViewState["CurrentRegionDisplayStyle"] != null)
                return (RegionStyle)this.ViewState["CurrentRegionDisplayStyle"];
            else
                return null;
        }
        set { this.ViewState["CurrentRegionDisplayStyle"] = value; }
    }

    [PersistenceModeAttribute(PersistenceMode.InnerProperty)]
    public RegionStyle RegionDisplayStyle
    {
        get
        {
            if (this.ViewState["RegionDisplayStyle"] != null)
                return (RegionStyle)this.ViewState["RegionDisplayStyle"];
            else
                return null;
        }
        set { this.ViewState["RegionDisplayStyle"] = value; }
    }

    public VObject CurrentVObject
    {
        get { return _canvasViewer.Canvas.CurrentVObject; }
    }

    public NamedRectangleRegion CurrentRegion
    {
        get
        {
            Layer l = _canvasViewer.Canvas.CurrentLayer;
            if (l == null || l.Region == null && !string.IsNullOrEmpty(l.Name))
                return null;
            else
                return new NamedRectangleRegion(l.Name, new RectangleF(l.Region.Left, l.Region.Top, l.Region.Width, l.Region.Height));
        }
        set
        {
            if (!string.IsNullOrEmpty(value.Name))
            {
                Layer[] l = _canvasViewer.Canvas.Layers.GetLayersByName(value.Name);
                if (l.Length > 0)
                    _canvasViewer.Canvas.CurrentLayerIndex = l[0].Index;
            }
            //set current style
            if (this.CurrentRegionDisplayStyle != null)
            {
                RegionStyle currentStyle = this.CurrentRegionDisplayStyle;
                RegionStyle regionStyle = this.RegionDisplayStyle ?? new RegionStyle();

                foreach (RectangleVObject vo in _regionLayer.VObjects)
                {
                    if (vo.Name == value.Name)
                    {
                        vo.BeginUpdate();
                        vo.BorderWidth = currentStyle.BorderWidth;
                        vo.BorderColor = currentStyle.BorderColor;
                        vo.FillColor = currentStyle.FillColor;
                        vo.EndUpdate();
                    }
                    else
                    {
                        vo.BeginUpdate();
                        vo.BorderWidth = regionStyle.BorderWidth;
                        vo.BorderColor = regionStyle.BorderColor;
                        vo.FillColor = regionStyle.FillColor;
                        vo.EndUpdate();
                    }
                }
            }
        }
    }

    public float Zoom
    {
        get { return _canvasViewer.Zoom; }
        set { _canvasViewer.Zoom = value; }
    }

    public Aurigma.GraphicsMill.AjaxControls.CanvasViewer CanvasViewer
    {
        get { return _canvasViewer; }
    }

    #endregion Public Properties

    private void DrawRegion(NamedRectangleRegion region)
    {
        RectangleVObject vo = new RectangleVObject(region.Left, region.Top, region.Width, region.Height);
        vo.BeginUpdate();
        vo.Name = region.Name;
        RegionStyle style = this.RegionDisplayStyle ?? new RegionStyle();
        vo.BorderWidth = style.BorderWidth;
        vo.BorderColor = style.BorderColor;
        vo.FillColor = style.FillColor;
        vo.Locked = true;
        vo.EndUpdate();
        _regionLayer.VObjects.Add(vo);
    }

    public void AddRegion(NamedRectangleRegion region)
    {
        Layer l = new Layer();
        l.Name = region.Name;
        l.Region = region;
        _canvasViewer.Canvas.Layers.Add(l);

        if (this.IsHighlightRegion)
            DrawRegion(region);
    }

    public void AddRegions(IEnumerable<NamedRectangleRegion> regions)
    {
        foreach (NamedRectangleRegion region in regions)
        {
            AddRegion(region);
        }
    }

    public void RemoveRegion(string regionName)
    {
        if (!string.IsNullOrEmpty(regionName))
        {
            //remove layer
            Layer[] layers = _canvasViewer.Canvas.Layers.GetLayersByName(regionName);
            Array.ForEach(layers, delegate(Layer l) { _canvasViewer.Canvas.Layers.Remove(l); });
            //remove highlight rectangle
            VObject[] voc = _regionLayer.VObjects.GetVObjectsByName(regionName);
            Array.ForEach(voc, delegate(VObject vo) { _regionLayer.VObjects.Remove(vo); });
        }
    }

    public void RemoveAllRegions()
    {
        for (int i = _canvasViewer.Canvas.Layers.Count - 1; i >= 0; i--)
        {
            Layer l = _canvasViewer.Canvas.Layers[i];
            if (l != _photoLayer && l != _regionLayer && !string.IsNullOrEmpty(l.Name))
            {
                _canvasViewer.Canvas.Layers.RemoveAt(i);
            }
        }
        _regionLayer.VObjects.Clear();
    }

    public IEnumerable<NamedRectangleRegion> GetRegions()
    {
        foreach (Layer l in _canvasViewer.Canvas.Layers)
        {
            if (l != _photoLayer && l != _regionLayer && !string.IsNullOrEmpty(l.Name))
                yield return new NamedRectangleRegion(l.Name, new RectangleF(l.Region.Left, l.Region.Top, l.Region.Width, l.Region.Height));
        }
    }

    public void AddVObject(VObject vo, string regionName)
    {
        if (string.IsNullOrEmpty(regionName))
            throw new ArgumentNullException("regionName");

        Layer[] layers = _canvasViewer.Canvas.Layers.GetLayersByName(regionName);
        if (layers.Length == 0)
        {
            // TODO: throw region not found exception
        }

        float dx = layers[0].Region.Left;
        float dy = layers[0].Region.Top;
        vo.Transform.TranslateX += dx;
        vo.Transform.TranslateY += dy;

        layers[0].VObjects.Add(vo);
    }

    public IEnumerable<VObject> GetVObjects()
    {
        foreach (Layer l in _canvasViewer.Canvas.Layers)
        {
            if (l != _photoLayer && l != _regionLayer && !string.IsNullOrEmpty(l.Name))
            {
                foreach (VObject vo in l.VObjects)
                    yield return vo;
            }
        }
    }

    public IEnumerable<VObject> GetVObjects(NamedRectangleRegion region)
    {
        return GetVObjects(region.Name);
    }

    public IEnumerable<VObject> GetVObjects(string regionName)
    {
        if (string.IsNullOrEmpty(regionName))
            throw new ArgumentNullException("regionName");

        Layer[] layers = _canvasViewer.Canvas.Layers.GetLayersByName(regionName);
        if (layers.Length > 0)
            foreach (VObject vo in layers[0].VObjects)
                yield return vo;
    }

    public void RemoveVObject(VObject vo)
    {
        foreach (Layer l in _canvasViewer.Canvas.Layers)
        {
            bool deleted = l.VObjects.Remove(vo);
            if (deleted) // object has been deleted, no reason to continue
                break;
        }
    }

    /// <summary>
    /// Return canvas data in JSON format.
    /// </summary>
    /// <param name="serializeBinaryData">Include image and color profile binary files in serialized string.</param>
    /// <returns>Serialized string.</returns>
    public string SaveState(bool serializeBinaryData)
    {
        if (serializeBinaryData)
           [h] return _canvasViewer.Canvas.Serialize(); [/h]
        else
            return _canvasViewer.Canvas.Data;
    }

    public void LoadState(string serializedData, bool withBinaryData)
    {
        if (withBinaryData)
         [h] _canvasViewer.Canvas.Deserialize(serializedData); [/h]
        else
            _canvasViewer.Canvas.Data = serializedData;
    }

    public Aurigma.GraphicsMill.Bitmap RenderRegion(string regionName, float dpi)
    {
        Layer renderedLayer = null;
        List<Layer> hiddenLayers = new List<Layer>(_canvasViewer.Canvas.Layers.Count);
        foreach (Layer l in _canvasViewer.Canvas.Layers)
        {
            if (l.Name == regionName)
            {
                renderedLayer = l;
            }
            else if (l.Visible)
            {
                l.Visible = false;
                hiddenLayers.Add(l);
            }
        }
        if (renderedLayer == null)
        {
            //TODO: throw exception
        }

        // Temporary remove region from rendered layer.
        RectangleRegion rr = renderedLayer.Region;
        renderedLayer.Region = null;

        //render canvas
        Aurigma.GraphicsMill.Bitmap bp = _canvasViewer.Canvas.RenderWorkspace(dpi, Aurigma.GraphicsMill.ColorSpace.Rgb);

        // Restore region back.
        renderedLayer.Region = rr;

        //crop region
        Rectangle r = new Rectangle(Common.ConvertPointsToPixels(dpi, renderedLayer.Region.Left),
            Common.ConvertPointsToPixels(dpi, renderedLayer.Region.Top),
            Common.ConvertPointsToPixels(dpi, renderedLayer.Region.Width),
            Common.ConvertPointsToPixels(dpi, renderedLayer.Region.Height));
        bp.Transforms.Crop(r);

        //revert hidden layers
        hiddenLayers.ForEach(delegate(Layer l) { l.Visible = true; });
        return bp;
    }

    public Aurigma.GraphicsMill.Bitmap RenderCanvas(float dpi)
    {
        Layer _regionLayer = this.CanvasViewer.Canvas.Layers.GetLayersByName(_regionLayerName)[0];
        SaveState(true);
        // Hide layer with region's rectangles.
        _regionLayer.Visible = false;

      [h] Bitmap bp = this.CanvasViewer.Canvas.RenderWorkspace(dpi, Aurigma.GraphicsMill.ColorSpace.Rgb); [/h]
        // Show regions layer back.
        _regionLayer.Visible = true;

        return bp;
    }

    #region IScriptControl Members

    public System.Collections.Generic.IEnumerable<ScriptDescriptor> GetScriptDescriptors()
    {
        ScriptControlDescriptor scd = new ScriptControlDescriptor("PhotoLabel.PhotoLabelControl", container.ClientID);
        scd.AddComponentProperty("canvasViewer", _canvasViewer.ClientID);
        scd.AddProperty("_photoLayerName", _photoLayer.Name);
        scd.AddProperty("_regionLayerName", _regionLayer.Name);
        scd.AddProperty("_zoomInNavigatorId", zoomInNavigator.ClientID);
        scd.AddProperty("_zoomOutNavigatorId", zoomOutNavigator.ClientID);
        scd.AddProperty("_panNavigatorId", panNavigator.ClientID);

        RegionStyle style = this.RegionDisplayStyle ?? new RegionStyle();
        scd.AddScriptProperty("_regionDisplayStyle", style.GetJSON());

        style = this.CurrentRegionDisplayStyle ?? new RegionStyle();
        scd.AddScriptProperty("_currentRegionDisplayStyle", style.GetJSON());

        return new ScriptDescriptor[] { scd };
    }

    public System.Collections.Generic.IEnumerable<ScriptReference> GetScriptReferences()
    {
        ScriptReference sr = new ScriptReference("Scripts/PhotoLabelControl.js");
        return new ScriptReference[] { sr };
    }

    #endregion
}



Thanks,
Won

Edited by user Monday, December 1, 2014 1:10:15 PM(UTC)  | Reason: Not specified

Fedor  
#8 Posted : Wednesday, December 3, 2014 11:49:40 PM(UTC)
Fedor

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 7/28/2003(UTC)
Posts: 1,660

Thanks: 5 times
Was thanked: 76 time(s) in 74 post(s)
Quote:
Code:
return _canvasViewer.Canvas.Serialize();


Code:
_canvasViewer.Canvas.Deserialize(serializedData);


We changed the way of serialization in Graphics Mill 7.x. Currently the library uses the SVG format with the ZIP compression instead of JSON. That's why you should use the binary Stream instead of the string to serialize/deserialize Canvas.

Canvas.Serialize
Canvas.Deserialize


Quote:
Code:
Bitmap bp = this.CanvasViewer.Canvas.RenderWorkspace(dpi, Aurigma.GraphicsMill.ColorSpace.Rgb);


Which exact error do you experience?
Best regards,
Fedor Skvortsov
wony  
#9 Posted : Thursday, December 4, 2014 7:03:47 AM(UTC)
wony

Rank: Newbie

Groups: Member
Joined: 11/25/2014(UTC)
Posts: 6

Fedor,


There are three places with errors.

Line from bleow: return _canvasViewer.Canvas.Serialize(); with error, "No overload for method 'Serialize' takes 0 arguments."
Code:

    public string SaveState(bool serializeBinaryData)
    {
        if (serializeBinaryData)
            return _canvasViewer.Canvas.Serialize();
        else
            return _canvasViewer.Canvas.Data;
    }



Line from below: _canvasViewer.Canvas.Deserialize(serializedData); with error, "The best overload method match for 'Ariguma.GraphicsMill.AjaxControls.VectorObjects.Canvas.Deserialize(System.IO.Stream)' has some invalid arguments."
Code:

    public void LoadState(string serializedData, bool withBinaryData)
    {
        if (withBinaryData)
            _canvasViewer.Canvas.Deserialize(serializedData);
        else
            _canvasViewer.Canvas.Data = serializedData;
    }



Line from below: this.CanvasViewer.Canvas.RenderWorkspace(dpi, Aurigma.GraphicsMill.ColorSpace.Rgb); with error, "Cannot implicitly convert type 'Aurigma.GraphicsMill.Bitmap'. An explicit conversion exists (are you missing a cast?)"
Code:

    public Aurigma.GraphicsMill.Bitmap RenderCanvas(float dpi)
    {
        Layer _regionLayer = this.CanvasViewer.Canvas.Layers.GetLayersByName(_regionLayerName)[0];
        SaveState(true);
        // Hide layer with region's rectangles.
        _regionLayer.Visible = false;

        Bitmap bp = this.CanvasViewer.Canvas.RenderWorkspace(dpi, Aurigma.GraphicsMill.ColorSpace.Rgb);

        // Show regions layer back.
        _regionLayer.Visible = true;

        return bp;
    }



I didn't originally write this code, and I am not familiar with graphics programming and GraphicsMill API. Changing some syntax I was able to do by looking at the reference, but rewriting the functionality in alternative way is difficult for me and frustrating.

Won
Fedor  
#10 Posted : Thursday, December 4, 2014 8:51:28 AM(UTC)
Fedor

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 7/28/2003(UTC)
Posts: 1,660

Thanks: 5 times
Was thanked: 76 time(s) in 74 post(s)
Quote:
I didn't originally write this code, and I am not familiar with graphics programming and GraphicsMill API. Changing some syntax I was able to do by looking at the reference, but rewriting the functionality in alternative way is difficult for me and frustrating.


Please open a case and send us the source code of your project. Our engineers will check it and make the changes.
Best regards,
Fedor Skvortsov
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.