Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
sir i want to recreate Ink stroke from bezierpoints of ink stroke already saved in db ,but
Iam facing type casting exception cant convert system.object to system.drawing.point.please suggest me how to convert system.object to system.drawing.point[] to recreate the stroke

code is as below
C#
foreach (Stroke S in inkOverlay.Ink.Strokes)
{
  SqlCommand inkcmd;
  cn = new SqlConnection("Data Source=localhost;Initial Catalog=amit;Integrated Security=True;Persist Security Info=True;Packet Size=4096");
  cn.Open();
  inkcmd = new SqlCommand("SELECT BPX,BPY FROM BezierTable where Alphabet= @Alphabet", this.sqlConnection1);
  SqlParameter param = new SqlParameter();
  param.ParameterName = "@Alphabet";
  param.Value = txtImage.Text;
  inkcmd.Parameters.Add(param);
  inkcmd.Connection = cn;
  ArrayList rowList = new ArrayList();
  rdr = inkcmd.ExecuteReader();
  While (rdr.Read())
  {
    object[] values = new object[rdr.FieldCount];
    rdr.GetValues(values);
    rowList.Add(values);
  }
  foreach (object[] row in rowList)
  {
     string[] bezierdetails = new string[row.Length];
     int columnIndex = 0;
     foreach (object column in row)
     {
          bezierdetails[columnIndex++] = Convert.ToString(column);
          Array x = rowList.ToArray();
          Point[] y = (Point[])x;//Typecasting error cant convert system.object to system.drawing.point,p
          Stroke newStroke = inkOverlay.Ink.CreateStroke(y);cant recreate stroke
          newStroke.Move(2000,300);
     }
  }
Posted
Updated 28-Jan-11 3:11am
v2

Instead of untyped and obsolete ArrayList use, for example, System.Collections.Generic.List<Point> rowList; your offending line will read: Point[] y = rowList.ToArray(); is this form it will compile because array element's exact type Point is defined by generic parameter.

—SA
 
Share this answer
 
v3
Comments
Nish Nishant 28-Jan-11 9:42am    
Proposed as answer, and voted 5.
Sergey Alexandrovich Kryukov 28-Jan-11 11:57am    
Thanks a lot, hopefully should lead to a resolution.
--SA
Sandeep Mewara 28-Jan-11 12:11pm    
Yes, good one... 5!
If you're dead set on an ArrayList, then you need to use the ArrayList.ToArray(Type) method

C#
Point[] y = (Point[])rowList.ToArray(typeof(Point))
 
Share this answer
 
Comments
googlejumbo 30-Jan-11 9:09am    
I have tried this code but new exceptions arises "At least one element in source array could not be cast down the destination array type" so please help me out for this error
William Winner 30-Jan-11 16:57pm    
Then you've added something to the array that isn't a Point.

Another good reason for the typed List from System.Collection.Generic. It would tell you if you tried to load something besides a point into it.
You can't cast something to what it is not.
Say you have
string name = "some arbitrary string";
object x = name;

This will get you nowhere:
int i = (int)x; // throws an exception
because the framework doesn't know how to convert it.

I didn't run your code (nothing to build it on right now), but you have to could loop through the values and convert them to Point individually. Stuff them in an IList<Point>, or array or whatever, after that.
 
Share this answer
 
v3
Comments
Nish Nishant 28-Jan-11 9:42am    
Voted 5.
Sergey Alexandrovich Kryukov 28-Jan-11 9:54am    
Indivara,
Your first example with "as" is pointless (regular assignment will work through boxing); this kind of dynamic cast is used for down-casting and returns null if not successful.
You should also explain that "(int)" cast will throw exception in your sample.
--SA
Indivara 28-Jan-11 19:05pm    
Thanks, fixed. I was multitasking and being lazy to write a full explanation...
Sergey Alexandrovich Kryukov 28-Jan-11 19:38pm    
All right. I actually just up-voted by 5.
--SA
William Winner 28-Jan-11 12:26pm    
not true...ArrayList.ToArray is overloaded and has an option for specifying the type.
You cannot directly convert an object of type object into a Point array.

Try something like -

C#
Point[] k = new Point[rowList.Count];
foreach(object o in rowList)
{
    k[i] = new Point(rdr[0],rdr[1]);
}


Note: This is pseudo code just to give you an idea. This will not work (nor compile).
 
Share this answer
 
Comments
William Winner 28-Jan-11 12:25pm    
Actually, that's not true. ArrayList.ToArray() is overloaded and has an option for specifying the type.

Though, your way is still completely valid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900