首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将byte[]转换为SAFEARRAY

将byte[]转换为SAFEARRAY
EN

Stack Overflow用户
提问于 2017-03-03 15:08:48
回答 1查看 646关注 0票数 0

我有个问题希望有人能帮上忙。用于在VS2012中编译的以下代码:

代码语言:javascript
复制
STDMETHODIMP CGatherStore::GetPathBmp(ULONGLONG sessionID, LONG *pWidth, LONG *pHeight, SAFEARRAY **pData)
{
  m_dbOps.OpenDatabase(m_depository);
  if (m_dbOps.HasPath(sessionID))
  {
    SessionData sd(m_dbOps.GetSessionPath(sessionID));

    *pWidth  = sd.pathHeader.bcWidth;
    *pHeight = sd.pathHeader.bcHeight;

    CComSafeArray<BYTE> bmpArray;
    CComSafeArrayBound  bounds;

    *pData = SafeArrayCreate(VT_UI1, 1, &bounds);
    if (sd.spPathRawData.m_p != NULL)
    {
      bmpArray.Attach(*pData);
      bmpArray.Add(sd.GetPathSize(), reinterpret_cast<BYTE *>(sd.spPathRawData.m_p), true);
      bmpArray.Detach();
    }
  }
  else
  {
    CComSafeArrayBound bounds;

    *pData = SafeArrayCreate(VT_UI1, 1, &bounds);
  }

  return S_OK;
}

出口申报如下:

代码语言:javascript
复制
void GetPathBmp(ulong sessionid, out int pWidth, out int pHeight, out Array pData);

调用它的函数如下:

代码语言:javascript
复制
public WriteableBitmap GetBitmapPath(ulong sessionID)
{
  WriteableBitmap bmp = null;

  try
  {
    int    width;
    int    height;
    byte[] data;

    gs.GetPathBmp(sessionID, out width, out height, out data);
    bmp = BitmapFactory.New(width, height);
    bmp.FromByteArray(data);
  }
  catch (Exception e)
  {
    System.Diagnostics.Trace.WriteLine(String.Format("GetBitmapPath failed, session ID {0} - {1}", sessionID, e.Message));
  }

  return bmp;
}

但是,当我试图在VS2015中编译它时,我会收到以下错误消息:

代码语言:javascript
复制
error CS1503: Argument 4: cannot convert from 'out byte[]' to 'out System.Array'

那么,如何将变量从byte[]转换为SAFEARRAY。

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-06 16:01:07

我将调用函数修改如下:

代码语言:javascript
复制
public WriteableBitmap GetBitmapPath(ulong sessionID)
{
  WriteableBitmap bmp = null;
  try
  {
    int          width;
    int          height;
    System.Array sa;

    gs.GetPathBmp(sessionID, out width, out height, out sa);
    bmp = BitmapFactory.New(width, height);

    byte[] data = new byte[sa.Length];
    sa.CopyTo(data, 0);
    bmp.FromByteArray(data);
  }
  catch (Exception e)
  {
    System.Diagnostics.Trace.WriteLine(String.Format("GetBitmapPath failed, session ID {0} - {1}", sessionID, e.Message));
  }
  return bmp;
}

好像很管用。谢谢大家的帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42582329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档