Author Topic: Setting image to clipboard problem.  (Read 2074 times)

0 Members and 1 Guest are viewing this topic.

BillZndl

  • Guest
Setting image to clipboard problem.
« on: September 22, 2015, 02:56:20 PM »
AutoCAD 2012, VS2010 Express, NET 3.5

Anyone have an idea of why after using the clipboard in the program,
autocad locks up as soon as I try to open a different drawing and displays the message:
"Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt”
The program works fine, multiple times, copying & pasting (using cntrl + V), until I try to open another drawing in SDI=1.

As soon as I rem out the clipboard line, all is well.
Do I need to dispose of something or clear the clipboard?

Code - C#: [Select]
  1.  private void button2_Click(object sender, EventArgs e)
  2.         {
  3.             try
  4.             {
  5.                 pictureBox1.Image = null;
  6.                 pictureBox1.Image = DrawPartInPicBox.PartImage(pictureBox1.Width, pictureBox1.Height); //creates bitmap image
  7.                 pictureBox1.Update();
  8.                 //Clipboard.SetImage(pictureBox1.Image); <------ this line is the culprit.
  9.             }
  10.             catch (Exception exc)
  11.             {
  12.                 MessageBox.Show("Error : " + exc.InnerException);
  13.             }
  14.            
  15.         }

TIA!

Bill

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Setting image to clipboard problem.
« Reply #1 on: September 22, 2015, 03:01:20 PM »
Can you publish the code sources of the simple"HelloWorld" project which exactly demonstrates your problem?
« Last Edit: September 22, 2015, 03:05:13 PM by Andrey Bushman »

BillZndl

  • Guest
Re: Setting image to clipboard problem.
« Reply #2 on: September 22, 2015, 03:43:56 PM »
Can you publish the code sources of the simple"HelloWorld" project which exactly demonstrates your problem?

Not easily.
But while looking things over, I discovered that I had created a definition of an ImageList, in the form code.

private ImageList ImgLst = new ImageList(); (no other reference to this in the code).

It never seemed to cause a problem until I added the line for the clipboard.setImage.
Now that I removed the imagelist reference, I can no longer duplicate the error.  :-(


I'll attach the netloadable .dll.
Command name : "ShowPic"

Simple form show with select entities button.
Displays selection in picturebox.Image.









mohnston

  • Bull Frog
  • Posts: 305
  • CAD Programmer
Re: Setting image to clipboard problem.
« Reply #3 on: September 22, 2015, 03:49:50 PM »
Is it possible that you have a reference to the image instead of a new image? Maybe try clone?
pictureBox1.Image = DrawPartInPicBox.PartImage(pictureBox1.Width, pictureBox1.Height).Clone();
It's amazing what you can do when you don't know what you can't do.
CAD Programming Solutions

BillZndl

  • Guest
Re: Setting image to clipboard problem.
« Reply #4 on: September 22, 2015, 04:05:52 PM »
Is it possible that you have a reference to the image instead of a new image? Maybe try clone?
pictureBox1.Image = DrawPartInPicBox.PartImage(pictureBox1.Width, pictureBox1.Height).Clone();

I don't think so.
The method "PartImage" returns a new bitmap image.

Code - C#: [Select]
  1. public static Bitmap PartImage(int width, int height)
  2.         {
  3.             ArrayList EntPnts = new ArrayList();
  4.             List<string> LaLst = new List<string> { };
  5.            
  6.             GetPointsFromEntityIDs(ref LaLst, ref EntPnts, width, height);   //points from AutoCAD entities.
  7.  
  8.             Bitmap image = new Bitmap(width, height);
  9.  
  10.             Graphics graphics = Graphics.FromImage(image);
  11.             graphics.Clear(Color.Black);
  12.  
  13.             if (EntPnts.Count == 0)  //empty list.
  14.             {
  15.                 graphics.DrawString("Nothing Selected: ", new System.Drawing.Font("Romans", 28f), Brushes.DarkCyan, (PointF)new Point(8, height / 2));
  16.             }
  17.  
  18.             if (EntPnts.Count > 0)  //converted pointF[]'s to draw.
  19.             {
  20.                 int num = 0;
  21.  
  22.                 foreach (PointF[] PntFarray in EntPnts)         //draw each entity from point set.
  23.                 {
  24.                     string Layer = LaLst[num];                  //color of layer entity is on.
  25.                     num++;                    
  26.  
  27.                     switch (Layer)
  28.                     {
  29.                         case "0":
  30.                         case "BEND":
  31.                             graphics.DrawLines(new Pen(Brushes.White), PntFarray);
  32.                             break;
  33.  
  34.                         case "LDRILL":
  35.                             graphics.DrawLines(new Pen(Brushes.Cyan), PntFarray);
  36.                             break;
  37.  
  38.                         case "NAIL":
  39.                             graphics.DrawLines(new Pen(Brushes.Yellow), PntFarray);
  40.                             break;
  41.  
  42.                         case "OUTSIDE":
  43.                             graphics.DrawLines(new Pen(Brushes.Red), PntFarray);
  44.                             break;
  45.  
  46.                         case "PH":
  47.                             graphics.DrawLines(new Pen(Brushes.OrangeRed), PntFarray);
  48.                             break;
  49.  
  50.                         case "CLEAR":
  51.                             graphics.DrawLines(new Pen(Brushes.Lime), PntFarray);
  52.                             break;
  53.  
  54.                         case "HID":
  55.                             graphics.DrawLines(new Pen(Brushes.Magenta), PntFarray);
  56.                             break;
  57.  
  58.                         default:
  59.                             graphics.DrawLines(new Pen(Brushes.White), PntFarray);
  60.                             break;
  61.                     }
  62.  
  63.                 }
  64.  
  65.                 graphics.Dispose();
  66.             }
  67.  
  68.             return image;
  69.         }