内存中有幅bmp图片 我怎么把它显示在picturebox

doudoushen 2008-05-07 03:27:27
vc可以用StretchDIBits函数解决 c#怎么写
...全文
1144 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhao_999 2010-04-10
  • 打赏
  • 举报
回复
很好!极具参考价值!
ericzhangbo1982111 2008-05-07
  • 打赏
  • 举报
回复
港好看到一片帖子


[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, int nWidth,
int nHeight, IntPtr hObjSource, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);

[DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
static extern IntPtr CreateCompatibleDC(IntPtr hdc);

[DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
static extern bool DeleteDC(IntPtr hdc);

[DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

[DllImport("gdi32.dll", ExactSpelling=true, SetLastError=true)]
static extern bool DeleteObject(IntPtr hObject);

public enum TernaryRasterOperations : uint {
SRCCOPY = 0x00CC0020,
SRCPAINT = 0x00EE0086,
SRCAND = 0x008800C6,
SRCINVERT = 0x00660046,
SRCERASE = 0x00440328,
NOTSRCCOPY = 0x00330008,
NOTSRCERASE = 0x001100A6,
MERGECOPY = 0x00C000CA,
MERGEPAINT = 0x00BB0226,
PATCOPY = 0x00F00021,
PATPAINT = 0x00FB0A09,
PATINVERT = 0x005A0049,
DSTINVERT = 0x00550009,
BLACKNESS = 0x00000042,
WHITENESS = 0x00FF0062
}

protected override void OnPaint(PaintEventArgs e) {
IntPtr pTarget = e.Graphics.GetHdc();
IntPtr pSource = CreateCompatibleDC(pTarget);
IntPtr pOrig = SelectObject(pSource, bmp.GetHbitmap());
BitBlt(pTarget, 0,0, bmp.Width, bmp.Height, pSource,0,0,TernaryRasterOperations.SRCCOPY);
IntPtr pNew = SelectObject(pSource, pOrig);
DeleteObject(pNew);
DeleteDC(pSource);
e.Graphics.ReleaseHdc(pTarget);
}
Choi57671452 2008-05-07
  • 打赏
  • 举报
回复
不用动用API吧。

/// <summary>
/// 将一组一维的字节数组转为图像。
/// </summary>
/// <exception cref="ArgumentNullException">Bytes 为 null 引用(在 Visual Basic 中为 Nothing)。</exception>
/// <exception cref="ArgumentException">Bytes 不包含图像数据。</exception>
/// <param name="Bytes">要转为图像的一组一维的字节数组。</param>
/// <returns>转换后的图像对象。</returns>
static public Bitmap BytesToBitmap(byte[] Bytes)
{
MemoryStream stream = null;
try
{
stream = new MemoryStream(Bytes);
return new Bitmap(stream);
}
catch (ArgumentNullException ex)
{
throw ex;
}
catch (ArgumentException ex)
{
throw ex;
}
finally
{
stream.Close();
}
}


异常是用了从类库里往外抛,你可以去掉。
ericzhangbo1982111 2008-05-07
  • 打赏
  • 举报
回复
算了
你自己复制吧
http://www.koders.com/csharp/fid677B8AA67BE0A20678580AFB3ED9805C3E1C79D2.aspx?s=FreeImage
gomoku 2008-05-07
  • 打赏
  • 举报
回复
1、写.Net程序就要尽量说.Net的话, 我在6楼不是给你.Net的方法了吗?

2、hdc怎么获得
任何Graphics对象都有GetHdc()方法.

ericzhangbo1982111 2008-05-07
  • 打赏
  • 举报
回复
// Memory I/O stream routines -----------------------------------------------

[DllImport(dllName, EntryPoint = "FreeImage_OpenMemory")]
public static extern FIMEMORY OpenMemory(IntPtr bits, Int32 size_in_bytes);

[DllImport(dllName, EntryPoint = "FreeImage_CloseMemory")]
public static extern void CloseMemory(FIMEMORY stream);

[DllImport(dllName, EntryPoint = "FreeImage_LoadFromMemory")]
public static extern FIBITMAP LoadFromMemory(FREE_IMAGE_FORMAT fif, FIMEMORY stream, int flags);

[DllImport(dllName, EntryPoint = "FreeImage_SaveToMemory")]
public static extern bool SaveToMemory(FREE_IMAGE_FORMAT fif, FIBITMAP dib, FIMEMORY stream, int flags);

[DllImport(dllName, EntryPoint = "FreeImage_TellMemory")]
public static extern long TellMemory(FIMEMORY stream, int flags);

[DllImport(dllName, EntryPoint = "FreeImage_SeekMemory")]
public static extern bool SeekMemory(FIMEMORY stream, long offset, int origin);

[DllImport(dllName, EntryPoint = "FreeImage_AcquireMemory")]
public static extern long AcquireMemory(FIMEMORY stream, ref IntPtr data, ref int size_in_bytes);


// Plugin interface -------------------------------------------

// missing FREE_IMAGE_FORMAT FreeImage_RegisterLocalPlugin(FI_InitProc proc_address,
// const char *format, const char *description,
// const char *extension, const char *regexpr);
//
// missing FREE_IMAGE_FORMAT FreeImage_RegisterExternalPlugin(const char *path,
// const char *format, const char *description,
// const char *extension, const char *regexpr);

[DllImport(dllName, EntryPoint = "FreeImage_GetFIFCount")]
public static extern int GetFIFCount();

[DllImport(dllName, EntryPoint = "FreeImage_SetPluginEnabled")]
public static extern int SetPluginEnabled(FREE_IMAGE_FORMAT format, bool enabled);

[DllImport(dllName, EntryPoint = "FreeImage_IsPluginEnabled")]
public static extern int IsPluginEnabled(FREE_IMAGE_FORMAT format);

[DllImport(dllName, EntryPoint = "FreeImage_GetFIFFromFormat")]
public static extern FREE_IMAGE_FORMAT GetFIFFromFormat(string format);

[DllImport(dllName, EntryPoint = "FreeImage_GetFIFFromMime")]
public static extern FREE_IMAGE_FORMAT GetFIFFromMime(string mime);

[DllImport(dllName, EntryPoint = "FreeImage_GetFormatFromFIF")]
public static extern string GetFormatFromFIF(FREE_IMAGE_FORMAT format);

[DllImport(dllName, EntryPoint = "FreeImage_GetFIFExtensionList")]
public static extern string GetFIFExtensionList(FREE_IMAGE_FORMAT format);

[DllImport(dllName, EntryPoint = "FreeImage_GetFIFDescription")]
public static extern string GetFIFDescription(FREE_IMAGE_FORMAT format);

[DllImport(dllName, EntryPoint = "FreeImage_GetFIFRegExpr")]
public static extern string GetFIFRegExpr(FREE_IMAGE_FORMAT format);

[DllImport(dllName, EntryPoint = "FreeImage_GetFIFFromFilename")]
public static extern FREE_IMAGE_FORMAT GetFIFFromFilename(string filename);

[DllImport(dllName, EntryPoint = "FreeImage_FIFSupportsReading")]
public static extern bool FIFSupportsReading(FREE_IMAGE_FORMAT format);

[DllImport(dllName, EntryPoint = "FreeImage_FIFSupportsWriting")]
public static extern bool FIFSupportsWriting(FREE_IMAGE_FORMAT format);

[DllImport(dllName, EntryPoint = "FreeImage_FIFSupportsExportBPP")]
public static extern bool FIFSupportsExportBPP(FREE_IMAGE_FORMAT format, int bpp);

[DllImport(dllName, EntryPoint = "FreeImage_FIFSupportsExportType")]
public static extern bool FIFSupportsExportType(FREE_IMAGE_FORMAT format, FREE_IMAGE_TYPE ftype);

[DllImport(dllName, EntryPoint = "FreeImage_FIFSupportsICCProfiles")]
public static extern bool FIFSupportsICCProfiles(FREE_IMAGE_FORMAT format, FREE_IMAGE_TYPE ftype);

// Multipage interface ----------------------------------------

[DllImport(dllName, EntryPoint = "FreeImage_OpenMultiBitmap")]
public static extern FIMULTIBITMAP OpenMultiBitmap(
FREE_IMAGE_FORMAT format, string filename, bool createNew, bool readOnly, bool keepCacheInMemory, int flags);

[DllImport(dllName, EntryPoint = "FreeImage_CloseMultiBitmap")]
public static extern long CloseMultiBitmap(FIMULTIBITMAP bitmap, int flags);

[DllImport(dllName, EntryPoint = "FreeImage_GetPageCount")]
public static extern int GetPageCount(FIMULTIBITMAP bitmap);

[DllImport(dllName, EntryPoint = "FreeImage_AppendPage")]
public static extern void AppendPage(FIMULTIBITMAP bitmap, FIBITMAP data);

[DllImport(dllName, EntryPoint = "FreeImage_InsertPage")]
public static extern void InsertPage(FIMULTIBITMAP bitmap, int page, FIBITMAP data);

[DllImport(dllName, EntryPoint = "FreeImage_DeletePage")]
public static extern void DeletePage(FIMULTIBITMAP bitmap, int page);

[DllImport(dllName, EntryPoint = "FreeImage_LockPage")]
public static extern FIBITMAP LockPage(FIMULTIBITMAP bitmap, int page);

[DllImport(dllName, EntryPoint = "FreeImage_UnlockPage")]
public static extern void UnlockPage(FIMULTIBITMAP bitmap, FIBITMAP data, bool changed);

[DllImport(dllName, EntryPoint = "FreeImage_MovePage")]
public static extern bool MovePage(FIMULTIBITMAP bitmap, int target, int source);

[DllImport(dllName, EntryPoint = "FreeImage_GetLockedPageNumbers")]
public static extern bool GetLockedPageNumbers(FIMULTIBITMAP bitmap, IntPtr pages, IntPtr count);

// File type request routines ---------------------------------

[DllImport(dllName, EntryPoint = "FreeImage_GetFileType")]
public static extern FREE_IMAGE_FORMAT GetFileType(string filename, int size);

// missing FREE_IMAGE_FORMAT FreeImage_GetFileTypeFromHandle(FreeImageIO *io,
// fi_handle handle, int size);

[DllImport(dllName, EntryPoint = "FreeImage_GetFileTypeFromMemory")]
public static extern FREE_IMAGE_FORMAT GetFileTypeFromMemory(FIMEMORY stream, int size);

// Image type request routines --------------------------------

[DllImport(dllName, EntryPoint = "FreeImage_GetImageType")]
public static extern FREE_IMAGE_TYPE GetImageType(FIBITMAP dib);

// FreeImage helper routines ------------------------------------------------

[DllImport(dllName, EntryPoint = "FreeImage_IsLittleEndian")]
public static extern bool IsLittleEndian();

[DllImport(dllName, EntryPoint = "FreeImage_LookupX11Color")]
public static extern bool LookupX11Color(string szColor, ref int red, ref int green, ref int blue);

[DllImport(dllName, EntryPoint = "FreeImage_LookupSVGColor")]
public static extern bool LookupSVGColor(string szColor, ref int red, ref int green, ref int blue);

// Pixel access functions -------------------------------------

[DllImport(dllName, EntryPoint = "FreeImage_GetBits")]
public static extern IntPtr GetBits(FIBITMAP dib);

[DllImport(dllName, EntryPoint = "FreeImage_GetScanLine")]
public static extern IntPtr GetScanLine(FIBITMAP dib, int scanline);

[DllImport(dllName, EntryPoint = "FreeImage_GetPixelIndex")]
public static extern bool GetPixelIndex(FIBITMAP dib, uint x, uint y, ref byte value);

[DllImport(dllName, EntryPoint = "FreeImage_GetPixelColor")]
public static extern bool GetPixelColor(FIBITMAP dib, uint x, uint y,
[Out, MarshalAs(UnmanagedType.LPStruct)]RGBQUAD value);
ericzhangbo1982111 2008-05-07
  • 打赏
  • 举报
回复
/** Image color type used in FreeImage.
*/
public enum FREE_IMAGE_COLOR_TYPE
{
FIC_MINISWHITE = 0, // min value is white
FIC_MINISBLACK = 1, // min value is black
FIC_RGB = 2, // RGB color model
FIC_PALETTE = 3, // color map indexed
FIC_RGBALPHA = 4, // RGB color model with alpha channel
FIC_CMYK = 5 // CMYK color model
};

/** Color quantization algorithms.
Constants used in FreeImage_ColorQuantize.
*/
public enum FREE_IMAGE_QUANTIZE
{
FIQ_WUQUANT = 0, // Xiaolin Wu color quantization algorithm
FIQ_NNQUANT = 1 // NeuQuant neural-net quantization algorithm by Anthony Dekker
}

/** Dithering algorithms.
Constants used in FreeImage_Dither.
*/
public enum FREE_IMAGE_DITHER
{
FID_FS = 0, // Floyd & Steinberg error diffusion
FID_BAYER4x4 = 1, // Bayer ordered dispersed dot dithering (order 2 dithering matrix)
FID_BAYER8x8 = 2, // Bayer ordered dispersed dot dithering (order 3 dithering matrix)
FID_CLUSTER6x6 = 3, // Ordered clustered dot dithering (order 3 - 6x6 matrix)
FID_CLUSTER8x8 = 4, // Ordered clustered dot dithering (order 4 - 8x8 matrix)
FID_CLUSTER16x16 = 5, // Ordered clustered dot dithering (order 8 - 16x16 matrix)
FID_BAYER16x16 = 6 // Bayer ordered dispersed dot dithering (order 4 dithering matrix)
}
/** Lossless JPEG transformations
Constants used in FreeImage_JPEGTransform
*/
public enum FREE_IMAGE_JPEG_OPERATION
{
FIJPEG_OP_NONE = 0, // no transformation
FIJPEG_OP_FLIP_H = 1, // horizontal flip
FIJPEG_OP_FLIP_V = 2, // vertical flip
FIJPEG_OP_TRANSPOSE = 3, // transpose across UL-to-LR axis
FIJPEG_OP_TRANSVERSE = 4, // transpose across UR-to-LL axis
FIJPEG_OP_ROTATE_90 = 5, // 90-degree clockwise rotation
FIJPEG_OP_ROTATE_180 = 6, // 180-degree rotation
FIJPEG_OP_ROTATE_270 = 7 // 270-degree clockwise (or 90 ccw)
};

/** Tone mapping operators.
Constants used in FreeImage_ToneMapping.
*/
public enum FREE_IMAGE_TMO
{
FITMO_DRAGO03 = 0, // Adaptive logarithmic mapping (F. Drago, 2003)
FITMO_REINHARD05 = 1, // Dynamic range reduction inspired by photoreceptor physiology (E. Reinhard, 2005)
};

/** Upsampling / downsampling filters.
Constants used in FreeImage_Rescale.
*/
public enum FREE_IMAGE_FILTER
{
FILTER_BOX = 0, // Box, pulse, Fourier window, 1st order (constant) b-spline
FILTER_BICUBIC = 1, // Mitchell & Netravali's two-param cubic filter
FILTER_BILINEAR = 2, // Bilinear filter
FILTER_BSPLINE = 3, // 4th order (cubic) b-spline
FILTER_CATMULLROM = 4, // Catmull-Rom spline, Overhauser spline
FILTER_LANCZOS3 = 5 // Lanczos3 filter
}

/** Color channels.
Constants used in color manipulation routines.
*/
public enum FREE_IMAGE_COLOR_CHANNEL
{
FICC_RGB = 0, // Use red, green and blue channels
FICC_RED = 1, // Use red channel
FICC_GREEN = 2, // Use green channel
FICC_BLUE = 3, // Use blue channel
FICC_ALPHA = 4, // Use alpha channel
FICC_BLACK = 5, // Use black channel
FICC_REAL = 6, // Complex images: use real part
FICC_IMAG = 7, // Complex images: use imaginary part
FICC_MAG = 8, // Complex images: use magnitude
FICC_PHASE = 9 // Complex images: use phase
}


// Message output function --------------------------------------------------

public delegate void FreeImage_OutputMessageFunction(FREE_IMAGE_FORMAT format, string msg);

/**
FreeImage API
See the FreeImage PDF documentation for a definition of each function.
*/
public class FreeImage
{
#if (DEBUG)
private const string dllName = "FreeImaged.dll";
#else
private const string dllName = "FreeImage.dll";
#endif

// Init/Error routines ----------------------------------------

[DllImport(dllName, EntryPoint = "FreeImage_Initialise")]
public static extern void Initialise(bool loadLocalPluginsOnly);

// alias for Americans :)
[DllImport(dllName, EntryPoint = "FreeImage_Initialise")]
public static extern void Initialize(bool loadLocalPluginsOnly);

[DllImport(dllName, EntryPoint = "FreeImage_DeInitialise")]
public static extern void DeInitialise();

// alias for Americians :)
[DllImport(dllName, EntryPoint = "FreeImage_DeInitialise")]
public static extern void DeInitialize();

// Version routines -------------------------------------------

[DllImport(dllName, EntryPoint = "FreeImage_GetVersion")]
public static extern string GetVersion();

[DllImport(dllName, EntryPoint = "FreeImage_GetCopyrightMessage")]
public static extern string GetCopyrightMessage();

// Message Output routines ------------------------------------

[DllImport(dllName, EntryPoint = "FreeImage_SetOutputMessage")]
public static extern void SetOutputMessage(FreeImage_OutputMessageFunction omf);

// Allocate / Clone / Unload routines ---------------------------------------

[DllImport(dllName, EntryPoint = "FreeImage_Allocate")]
public static extern FIBITMAP Allocate(int width, int height,
int bpp, uint red_mask, uint green_mask, uint blue_mask);

[DllImport(dllName, EntryPoint = "FreeImage_AllocateT")]
public static extern FIBITMAP AllocateT(FREE_IMAGE_TYPE ftype, int width,
int height, int bpp, uint red_mask, uint green_mask, uint blue_mask);

[DllImport(dllName, EntryPoint = "FreeImage_Clone")]
public static extern FIBITMAP Clone(FIBITMAP dib);

[DllImport(dllName, EntryPoint = "FreeImage_Unload")]
public static extern void Unload(FIBITMAP dib);

// Load / Save routines -----------------------------------------------------

[DllImport(dllName, EntryPoint = "FreeImage_Load")]
public static extern FIBITMAP Load(FREE_IMAGE_FORMAT format, string filename, int flags);

// missing FIBITMAP FreeImage_LoadFromHandle(FREE_IMAGE_FORMAT fif,
// FreeImageIO *io, fi_handle handle, int flags);

[DllImport(dllName, EntryPoint = "FreeImage_Save")]
public static extern bool Save(FREE_IMAGE_FORMAT format, FIBITMAP dib, string filename, int flags);

// missing BOOL FreeImage_SaveToHandle(FREE_IMAGE_FORMAT fif, FIBITMAP *dib,
// FreeImageIO *io, fi_handle handle, int flags);
ericzhangbo1982111 2008-05-07
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsApplication1
{

/**
Handle to a bitmap
*/
using FIBITMAP = UInt32;
/**
Handle to a multipage file
*/
using FIMULTIBITMAP = UInt32;
/**
Handle to a memory I/O stream
*/
using FIMEMORY = UInt32;
using System.Runtime.InteropServices;



[StructLayout(LayoutKind.Sequential)]
public class RGBQUAD
{
public byte rgbBlue;
public byte rgbGreen;
public byte rgbRed;
public byte rgbReserved;
}

[StructLayout(LayoutKind.Sequential)]
public class RGBTRIPLE
{
public byte rgbtBlue;
public byte rgbtGreen;
public byte rgbtRed;
}

// Types used in the library (specific to FreeImage) ------------------------
/** 48-bit RGB
*/
[StructLayout(LayoutKind.Sequential)]
public class FIRGB16
{
public ushort red;
public ushort green;
public ushort blue;
}

/** 64-bit RGBA
*/
[StructLayout(LayoutKind.Sequential)]
public class FIRGBA16
{
public ushort red;
public ushort green;
public ushort blue;
public ushort alpha;
}

/** 96-bit RGB Float
*/
[StructLayout(LayoutKind.Sequential)]
public class FIRGBF
{
public float red;
public float green;
public float blue;
}

/** 128-bit RGBA Float
*/
[StructLayout(LayoutKind.Sequential)]
public class FIRGBAF
{
public float red;
public float green;
public float blue;
public float alpha;
}

/** Data structure for COMPLEX type (complex number)
*/
[StructLayout(LayoutKind.Sequential)]
public class FICOMPLEX
{
// real part
public double r;
// imaginary part
public double i;
}

// ICC profile support ------------------------------------------------------

public enum ICCFlags
{
FIICC_DEFAULT = 0x00,
FIICC_COLOR_IS_CMYK = 0x01
}

[StructLayout(LayoutKind.Sequential)]
public class FIICCPROFILE
{
public ushort flags; // info flag
public uint size; // profile's size measured in bytes
public IntPtr data; // points to a block of contiguous memory containing the profile
}

// Important enums ----------------------------------------------------------

public enum LoadSaveFlags
{
BMP_DEFAULT = 0,
BMP_SAVE_RLE = 1,
CUT_DEFAULT = 0,
DDS_DEFAULT = 0,
FAXG3_DEFAULT = 0,
GIF_DEFAULT = 0,
GIF_LOAD256 = 1, // Load the image as a 256 color image with ununsed palette entries, if it's 16 or 2 color
GIF_PLAYBACK = 2, // 'Play' the GIF to generate each frame (as 32bpp) instead of returning raw frame data when loading
HDR_DEFAULT = 0,
ICO_DEFAULT = 0,
ICO_MAKEALPHA = 1, // convert to 32bpp and create an alpha channel from the AND-mask when loading
IFF_DEFAULT = 0,
JPEG_DEFAULT = 0,
JPEG_FAST = 1,
JPEG_ACCURATE = 2,
JPEG_QUALITYSUPERB = 0x80,
JPEG_QUALITYGOOD = 0x100,
JPEG_QUALITYNORMAL = 0x200,
JPEG_QUALITYAVERAGE = 0x400,
JPEG_QUALITYBAD = 0x800,
JPEG_CMYK = 0x1000, // load separated CMYK "as is" (use | to combine with other flags)
KOALA_DEFAULT = 0,
LBM_DEFAULT = 0,
MNG_DEFAULT = 0,
PCD_DEFAULT = 0,
PCD_BASE = 1, // load the bitmap sized 768 x 512
PCD_BASEDIV4 = 2, // load the bitmap sized 384 x 256
PCD_BASEDIV16 = 3, // load the bitmap sized 192 x 128
PCX_DEFAULT = 0,
PNG_DEFAULT = 0,
PNG_IGNOREGAMMA = 1, // avoid gamma correction
PNM_DEFAULT = 0,
PNM_SAVE_RAW = 0, // If set the writer saves in RAW format (i.e. P4, P5 or P6)
PNM_SAVE_ASCII = 1, // If set the writer saves in ASCII format (i.e. P1, P2 or P3)
PSD_DEFAULT = 0,
RAS_DEFAULT = 0,
TARGA_DEFAULT = 0,
TARGA_LOAD_RGB888 = 1, // If set the loader converts RGB555 and ARGB8888 -> RGB888.
TIFF_DEFAULT = 0,
TIFF_CMYK = 0x0001, // reads/stores tags for separated CMYK (use | to combine with compression flags)
TIFF_PACKBITS = 0x0100, // save using PACKBITS compression
TIFF_DEFLATE = 0x0200, // save using DEFLATE compression (a.k.a. ZLIB compression)
TIFF_ADOBE_DEFLATE = 0x0400, // save using ADOBE DEFLATE compression
TIFF_NONE = 0x0800, // save without any compression
TIFF_CCITTFAX3 = 0x1000, // save using CCITT Group 3 fax encoding
TIFF_CCITTFAX4 = 0x2000, // save using CCITT Group 4 fax encoding
TIFF_LZW = 0x4000, // save using LZW compression
TIFF_JPEG = 0x8000, // save using JPEG compression
WBMP_DEFAULT = 0,
XBM_DEFAULT = 0,
XPM_DEFAULT = 0,
}

/** I/O image format identifiers.
*/
public enum FREE_IMAGE_FORMAT
{
FIF_UNKNOWN = -1,
FIF_BMP = 0,
FIF_ICO = 1,
FIF_JPEG = 2,
FIF_JNG = 3,
FIF_KOALA = 4,
FIF_LBM = 5,
FIF_IFF = FIF_LBM,
FIF_MNG = 6,
FIF_PBM = 7,
FIF_PBMRAW = 8,
FIF_PCD = 9,
FIF_PCX = 10,
FIF_PGM = 11,
FIF_PGMRAW = 12,
FIF_PNG = 13,
FIF_PPM = 14,
FIF_PPMRAW = 15,
FIF_RAS = 16,
FIF_TARGA = 17,
FIF_TIFF = 18,
FIF_WBMP = 19,
FIF_PSD = 20,
FIF_CUT = 21,
FIF_XBM = 22,
FIF_XPM = 23,
FIF_DDS = 24,
FIF_GIF = 25,
FIF_HDR = 26,
FIF_FAXG3 = 27,
FIF_SGI = 28
}

/** Image type used in FreeImage.
*/
public enum FREE_IMAGE_TYPE
{
FIT_UNKNOWN = 0, // unknown type
FIT_BITMAP = 1, // standard image : 1-, 4-, 8-, 16-, 24-, 32-bit
FIT_UINT16 = 2, // array of unsigned short : unsigned 16-bit
FIT_INT16 = 3, // array of short : signed 16-bit
FIT_UINT32 = 4, // array of unsigned long : unsigned 32-bit
FIT_INT32 = 5, // array of long : signed 32-bit
FIT_FLOAT = 6, // array of float : 32-bit IEEE floating point
FIT_DOUBLE = 7, // array of double : 64-bit IEEE floating point
FIT_COMPLEX = 8, // array of FICOMPLEX : 2 x 64-bit IEEE floating point
FIT_RGB16 = 9, // 48-bit RGB image : 3 x 16-bit
FIT_RGBA16 = 10, // 64-bit RGBA image : 4 x 16-bit
FIT_RGBF = 11, // 96-bit RGB float image : 3 x 32-bit IEEE floating point
FIT_RGBAF = 12 // 128-bit RGBA float image : 4 x 32-bit IEEE floating point
}

ericzhangbo1982111 2008-05-07
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

using WindowsAppliaction1;


namespace FI
{

public class Form1 : System.Windows.Forms.Form
{
private UInt32 fi;

private void Form1_Load(object sender, System.EventArgs e)
{
string img = @"C:\Temp\kodim22.png";

this.fi = FreeImage.Load(FREE_IMAGE_FORMAT.FIF_PNG, img, 0);
}

[DllImport("Gdi32.dll")]
private static extern int SetStretchBltMode(IntPtr HDC, int iStretchMode);

[DllImport("Gdi32.dll")]
private static extern int StretchDIBits(
IntPtr HDC,
int XDest, // x-coord of destination upper-left corner
int YDest, // y-coord of destination upper-left corner
int nDestWidth, // width of destination rectangle
int nDestHeight, // height of destination rectangle
int XSrc, // x-coord of source upper-left corner
int YSrc, // y-coord of source upper-left corner
int nSrcWidth, // width of source rectangle
int nSrcHeight, // height of source rectangle
IntPtr lpBits, // bitmap bits
BITMAPINFO lpBitsInfo, // bitmap data
uint iUsage, // usage options
int dwRop // raster operation code
);


private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
IntPtr hdc = e.Graphics.GetHdc();

int r = SetStretchBltMode(hdc, 3 /* COLORONCOLOR */);
r = StretchDIBits(hdc,
0, 0, this.ClientSize.Width, this.ClientSize.Height,
0, 0, (int)FreeImage.GetWidth(this.fi), (int)FreeImage.GetHeight(this.fi),
FreeImage.GetBits(this.fi),
FreeImage.GetInfo(this.fi),
0 /* DIB_RGB_COLORS */, 0x00CC0020 /* SRCCOPY */);

e.Graphics.ReleaseHdc(hdc);
}
}

}


真的不想写这个玩意


doudoushen 2008-05-07
  • 打赏
  • 举报
回复
1楼ericzhangbo1982111不是给你建议了吗?


问题是
hdc这个dc怎么获得


struct BITMAPINFO
{
public int biSize; // 本结构所占用字节数
   public int biWidth; // 位图的宽度,以像素为单位
   public int biHeight; // 位图的高度,以像素为单位
   public short biPlanes; // 目标设备的级别,必须为1
public short biBitCount;// 每个像素所需的位数,必须是1(双色),
  // 4(16色),8(256色)或24(真彩色)之一
   public int biCompression; // 位图压缩类型,必须是 0(不压缩),
  // 1(BI_RLE8压缩类型)或2(BI_RLE4压缩类型)之一
   public short biSizeImage; // 位图的大小,以字节为单位
   public int biXPelsPerMeter; // 位图水平分辨率,每米像素数
   public int biYPelsPerMeter; // 位图垂直分辨率,每米像素数
   public int biClrUsed;// 位图实际使用的颜色表中的颜色数
   public int biClrImportant;// 位图显示过程中重要的颜色数


public byte rgbBlue;// 蓝色的亮度(值范围为0-255)
   public byte rgbGreen; // 绿色的亮度(值范围为0-255)
   public byte rgbRed; // 红色的亮度(值范围为0-255)
   public byte rgbReserved;// 保留,必须为0

};
//数据流回调函数
public static void StreamNotify(byte [] pDIBHead, byte [] pDIBits)
{

BITMAPINFO bmpinfo=(BITMAPINFO)pDIBHead;
DDStream.StretchDIBits(hdc,
0, 0, HForm.hform.pictureBox1.Width ,
HForm.hform.pictureBox1.Height ,
0, 0, bmpinfo.biWidth , bmpinfo.biHeight ,
pDIBits, bmpinfo, 0 /*DIB_RGB_COLORS*/, 0xCC0020);
}


出错:
1、BITMAPINFO bmpinfo=(BITMAPINFO)pDIBHead;//不能牵制着转换
2、hdc怎么获得
gomoku 2008-05-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 doudoushen 的回复:]
楼上各位,我图片是从回调函数中取得 都是byte
而Bitmap a=new Bitmap (需要stream类型)
我怎么byte->stream????
[/Quote]

1楼ericzhangbo1982111不是给你建议了吗?


byte[] rawData;
GCHandle gch = GCHandle.Alloc(raw, GCHandleType.Pinned);
IntPtr scan0 = gch.AddrOfPinnedObject();
Bitmap bmp = new Bitmap(128, 128, 128 * 3, System.Drawing.Imaging.PixelFormat.Format24bppRgb, scan0);
gch.Free();


doudoushen 2008-05-07
  • 打赏
  • 举报
回复
楼上各位,我图片是从回调函数中取得 都是byte
而Bitmap a=new Bitmap (需要stream类型)
我怎么byte->stream????
楼外楼 2008-05-07
  • 打赏
  • 举报
回复
Image.From(MemoryStream)
owennol 2008-05-07
  • 打赏
  • 举报
回复
内存的图像应该是在一个Stream中;

Bitmap bm = new Bitmap(stream);
PictureBox.Image = bm;
gomoku 2008-05-07
  • 打赏
  • 举报
回复

PictureBox p;
Bitmap bmp;

using (Graphics g = p.CreateGraphics())
{
g.DrawImage(bmp, new Point(0, 0)); // =StretchDIBits
}


// 更简单的
p.Image = bmp;
ericzhangbo1982111 2008-05-07
  • 打赏
  • 举报
回复
StretchDIBits

[DllImport("gdi32.dll")]
static extern int StretchDIBits(IntPtr hdc, int XDest, int YDest,
int nDestWidth, int nDestHeight, int XSrc, int YSrc, int nSrcWidth,
int nSrcHeight, byte [] lpBits, [In] ref BITMAPINFO lpBitsInfo, uint iUsage,
uint dwRop);

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧