Security permisions for unmanaged code in .Net Applet
-
I'm developing - simple graphics editor, that should be placed on the web-site. It downloads the picture from the MSSQL server (i do this via WebService), edits this picture (putting some lines, dots, whatever), and than uploads changes to server. Since 2 part are ready, i have a terrible headache about the lastthing. The problem is: I draw primitives using Graphics, since this class don't know how to save the image drawn - I have to use unmanaged code to save it (BitBlt and stuff). Everything works ok, if it's standalone application - but i MUST use it as Applet,so there is a SecurityPermision Exception is raised when i'm trying to do unmanaged code. I tried to add the signature to the dll (sn -k), but somewhy IE doesn't want to load this applet. As far as i know, if user adds my DLL's public key to the trusted list - i can do unmanaged code (where he should put it). So the questions are: a) How i can save the image with drawn primitives (actually i need a stream to it, to upload to the server) using managed code b) How should add sign a dll, to let IE show it c) How should user add this to his trusted Assemblies list Here are the source codes: index.html IEControlv2.cs (windows control) new SecurityPermission(SecurityPermissionFlag.AllFlags).Assert(); CreateBitmapFromGraphics(gr,1,1,100,100); <---unmanaged code, where exception is raised. CodeAccessPermission.RevertAssert(); P.s. the .Net is 1.1 version
-
I'm developing - simple graphics editor, that should be placed on the web-site. It downloads the picture from the MSSQL server (i do this via WebService), edits this picture (putting some lines, dots, whatever), and than uploads changes to server. Since 2 part are ready, i have a terrible headache about the lastthing. The problem is: I draw primitives using Graphics, since this class don't know how to save the image drawn - I have to use unmanaged code to save it (BitBlt and stuff). Everything works ok, if it's standalone application - but i MUST use it as Applet,so there is a SecurityPermision Exception is raised when i'm trying to do unmanaged code. I tried to add the signature to the dll (sn -k), but somewhy IE doesn't want to load this applet. As far as i know, if user adds my DLL's public key to the trusted list - i can do unmanaged code (where he should put it). So the questions are: a) How i can save the image with drawn primitives (actually i need a stream to it, to upload to the server) using managed code b) How should add sign a dll, to let IE show it c) How should user add this to his trusted Assemblies list Here are the source codes: index.html IEControlv2.cs (windows control) new SecurityPermission(SecurityPermissionFlag.AllFlags).Assert(); CreateBitmapFromGraphics(gr,1,1,100,100); <---unmanaged code, where exception is raised. CodeAccessPermission.RevertAssert(); P.s. the .Net is 1.1 version
The only way to do what you want here without using your unmanaged DLL is to create a Bitmap object and draw to it. Example:
private Bitmap bmpCanvas; OnLoad(...) { bmpCanvas = new Bitmap(this.ClientSize.Width, this.ClientSize.Height); } OnResize(...) { bmpCanvas = new Bitmap(this.ClientSize.Width, this.ClientSize.Height); } OnDrawRect(...) { Graphics g = Graphics.FromImage(bmpCanvas); g.DrawRectangle(...); g.Dispose(); } OnPaint(object sender, PaintEventArgs pe) { pe.Graphics.DrawImage(bmpCanvas, 0, 0); }
After this has been implemented you can start streaming fairly easily: // Pre-Buffer alittle maybe: MemoryStream stream = new MemoryStream(bmpCanvas.Width * cmpCanvas.Height * 3); // Stream as PNG (loss-less and has good compression): bmpCanvas.SaveImage(stream, ImageFormat.Png); You can then call stream.GetBuffer() and use stream.Length to send the data. You might want to ensure the user is complete and dispose of the Bitmap to reduce the memory footprint. -
The only way to do what you want here without using your unmanaged DLL is to create a Bitmap object and draw to it. Example:
private Bitmap bmpCanvas; OnLoad(...) { bmpCanvas = new Bitmap(this.ClientSize.Width, this.ClientSize.Height); } OnResize(...) { bmpCanvas = new Bitmap(this.ClientSize.Width, this.ClientSize.Height); } OnDrawRect(...) { Graphics g = Graphics.FromImage(bmpCanvas); g.DrawRectangle(...); g.Dispose(); } OnPaint(object sender, PaintEventArgs pe) { pe.Graphics.DrawImage(bmpCanvas, 0, 0); }
After this has been implemented you can start streaming fairly easily: // Pre-Buffer alittle maybe: MemoryStream stream = new MemoryStream(bmpCanvas.Width * cmpCanvas.Height * 3); // Stream as PNG (loss-less and has good compression): bmpCanvas.SaveImage(stream, ImageFormat.Png); You can then call stream.GetBuffer() and use stream.Length to send the data. You might want to ensure the user is complete and dispose of the Bitmap to reduce the memory footprint.dnewmon, GREAT thanks - your solution killed few another rabbits :) I have only one problem - Eraser tool. So, assuming, i have the Bitmap - with the Transperent background. I draw on this Bitmap primitives - i want to be able somehow to erase their parts. Let's say i draw few lines, DrawLine(pen,1,1,100,100); DrawLine(pen,10,10,100,100); and want them to dissapear in the Rectangle(90,90,10,10); How can i do that? Is the Bitmap::LockBits method relevant? great 10x
-
dnewmon, GREAT thanks - your solution killed few another rabbits :) I have only one problem - Eraser tool. So, assuming, i have the Bitmap - with the Transperent background. I draw on this Bitmap primitives - i want to be able somehow to erase their parts. Let's say i draw few lines, DrawLine(pen,1,1,100,100); DrawLine(pen,10,10,100,100); and want them to dissapear in the Rectangle(90,90,10,10); How can i do that? Is the Bitmap::LockBits method relevant? great 10x
I just figured out a way to do this:
bmpCanvas = new Bitmap(320, 240, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bmpCanvas); g.Clear(Color.White); g.FillRectangle(new SolidBrush(Color.Red), 0, 0, 200, 200); g.FillEllipse(new SolidBrush(Color.FromArgb(255, 0, 0, 0)), 0, 0, 200, 200); g.Dispose(); bmpCanvas.MakeTransparent(Color.FromArgb(255, 0, 0, 0));
The ellipse is an "erased" region and considered transparent. I tried using Color.Transparent instead of Color.FromArgb(255, 0, 0, 0) but had no luck. NOTE: The MakeTransparent(...) call has to be made every time you erase a region. Enjoy, David -
I just figured out a way to do this:
bmpCanvas = new Bitmap(320, 240, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bmpCanvas); g.Clear(Color.White); g.FillRectangle(new SolidBrush(Color.Red), 0, 0, 200, 200); g.FillEllipse(new SolidBrush(Color.FromArgb(255, 0, 0, 0)), 0, 0, 200, 200); g.Dispose(); bmpCanvas.MakeTransparent(Color.FromArgb(255, 0, 0, 0));
The ellipse is an "erased" region and considered transparent. I tried using Color.Transparent instead of Color.FromArgb(255, 0, 0, 0) but had no luck. NOTE: The MakeTransparent(...) call has to be made every time you erase a region. Enjoy, David