类型转换不能将sysetm.object转换为system.drawing .point [] [英] Type casting cant convert sysetm.object to system.drawing .point[]

查看:67
本文介绍了类型转换不能将sysetm.object转换为system.drawing .point []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生我想从已经保存在db中的墨水笔划的bezier点重新创建墨迹笔划,但是

Iam面临类型转换异常,无法将system.object转换为system.drawing.point.please建议我如何将system.object转换为system.drawing.point []以重新创建笔画



代码如下

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

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);
     }
  }

推荐答案

而不是无类型和过时 ArrayList 使用,例如, System.Collections.Generic.List< Point> rowList ;您的违规行将显示为: Point [] y = rowList.ToArray();是这个形式它将编译因为数组元素的确切类型由泛型参数定义。



-SA
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


如果你在ArrayList上设置了死,那么你需要使用 ArrayList.ToArray(Type)方法



If you're dead set on an ArrayList, then you need to use the ArrayList.ToArray(Type) method

Point[] y = (Point[])rowList.ToArray(typeof(Point))


你不能把东西投射到它不是什么。

假设你有

You can't cast something to what it is not.
Say you have
string name = "some arbitrary string";
object x = name;



这将无处可寻:

int i =( INT)X; //抛出异常

因为框架不知道如何转换它。



我没有运行你的代码(现在没有任何东西可以构建它),但你必须可以遍历这些值并将它们单独转换为 Point 。在 IList< Point> ,或数组或其他任何内容之后填充它们。


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.


这篇关于类型转换不能将sysetm.object转换为system.drawing .point []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆