C++/C#类型笔记

(void *)和(byte *) 有区别么?

void* : 类属指针
byte* : (byte ->> typedef unsigned char byte)

===》C# 

Byte[]

C# Bitmap类型与Byte[]类型相互转化

Bitmap   =>   byte[] 
Bitmap   b   =   new   Bitmap( "test.bmp "); 
MemoryStream   ms   =   new   MemoryStream(); 
b.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp); 
byte[]   bytes=   ms.GetBuffer();  //byte[]   bytes=   ms.ToArray(); 这两句都可以,至于区别么,下面有解释
ms.Close(); 

byte[]    =>   Bitmap
byte[]   bytelist=bytes; 
MemoryStream   ms1   =   new   MemoryStream(bytelist); 
Bitmap   bm   =   (Bitmap)Image.FromStream(ms1); 
ms1.Close(); 


1、因为如果不用Bmp的方式转换字节的话,在转换到字节的时候将会丢失数据; 
2、MemoryStream的GetBuffer并不是得到这个流所存储的内容,而是返回这个流的基础字节数组,可能包括在扩充的时候一些没有使用到的字节。

如果用了MemoryStream,就不能说效果差不多。因为我用java重新开发了MemoryStream这个类,所以象这样的并且应该注意的问题基本上都很清楚,其中GetBuffer就是,有很多人认为用这个方法就是得到写入MemoryStream的字节,其实不是这样的。因为MemoryStream是用byte[]来存储数据,在写入数据的时候就有个扩充的过程,也就是说这个流的长度并不是它的byte[]字段的长度。而GetBuffer返回的就是byte[]这个字段。
下面是GetBuffer的部分实现方法,也许看了这个会更明白: 
public   byte[]   GetBuffer()   { 
        return   this._buffer; 

下面是ToArray的实现方法: 
public   byte[]   ToArray()   { 
        byte[]   bs   =   new   byte[this._length   -   this._origin]; 
        for   (int   i   =   this._origin,   j   =   0;   i   <   this._length;   i++)
                bs[j++]   =   this._buffer[i]; 
        return   bs; 

File.WriteAllBytes(fileName, imageData);

If the array contains only raw pixel data, you can create a Bitmap object using the data:

unsafe {
   fixed (byte* ptr = imageData) {
      using (Bitmap image = new Bitmap(width, height, stride, PixelFormat.Format24bppRgb, new IntPtr(ptr))) {
         image.Save(fileName);
      }
   }
}



Code.google.com.cn
RAW 灰度代码大全。

http://dev.mjxy.cn/a-C-Sharp-RGB-to-Palette-Based-8-bit-Greyscale-Bitmap-Class.aspx


http://www.devsource.com/c/a/Languages/Image-Processing-in-C/ 

开发源代码:


C:\Windows\twain_32 的Sample包含模拟图片。


本期全力学习C/C++,LINUX。UBUNTU


Adobe Reader 只是阅读器,不能进行转换的操作。但有很多软件可以实现这个功能,Adobe acrobat professional、ScanSoft PDF Professional 以及Foxit PDF editor都可以。 
  • I received a DLL which uses BSTR as string type:

    example : given in the include file

    DLLEXPORT BSTR WINAPI I2cGetInterfaceTypeStr (BSTR Interface);

    how do i access this function in vc#. This doesn't work.

    [DllImport("I2CAPI32.dll")] public static extern string I2cGetInterfaceTypeStr (string Interface);

    Other functions of the DLL which don't use BSTR are working ok.

    Can anyone help me.

    Thanks, Jan

答案

  • 2005年7月6日 22:45
    anfortas 的头像
    175 Points
     
      已答复
    It's pretty unusual to see a function directly returning a BSTR like that, but I think this should work:
       
    [DllImport("I2CAPI32.dll", CharSet=CharSet.Unicode)]
    [return: MarshalAs(UnmanagedType.BStr)]
    public static extern string I2cGetInterfaceTypeStr([MarshalAs(UnmanagedType.BStr)] string Interface);

     

全部回复

  • 2005年6月7日 15:51
    Kaetemi 的头像
    195 Points
     
     
    There is something about BSTR on this link:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkCOMInteropPart1CClientTutorial.asp

    This might help you a little further on finding how to use what your are trying to do :)
  • 2005年6月26日 15:48
    MigrationUser 1 的头像
    735 Points
     
     
    Change --> public static extern string
    into
    public static extern IntPtr
    and use the Marshal.PtrToStringBSTR to copy the BSTR to a managed string.
     
    Willy.
     
    I received a DLL which uses BSTR as string type:

    example : given in the include file

    DLLEXPORT BSTR WINAPI I2cGetInterfaceTypeStr (BSTR Interface);

    how do i access this function in vc#. This doesn't work.

    [DllImport("I2CAPI32.dll")] public static extern string I2cGetInterfaceTypeStr (string Interface);

    Other functions of the DLL which don't use BSTR are working ok.

    Can anyone help me.

    Thanks, Jan

     

     

  • 2005年7月6日 22:45
    anfortas 的头像
    175 Points
     
      已答复
    It's pretty unusual to see a function directly returning a BSTR like that, but I think this should work:
       
    [DllImport("I2CAPI32.dll", CharSet=CharSet.Unicode)]
    [return: MarshalAs(UnmanagedType.BStr)]
    public static extern string I2cGetInterfaceTypeStr([MarshalAs(UnmanagedType.BStr)] string Interface);

     
  • 2010年6月29日 2:22
    DrShin 的头像
    0 Points
     
     

    cool~

    thank u~


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值