没用过,看看msdn的实例吧~~http://msdn.microsoft.com/zh-cn/library/5ey6h79d(v=vs.110).aspx
解决方案 »
- 运行winform程序出现。开发工具是vs2008
- 用c#写一个记事本程序,用了richtextbox控件,可是发现无法用输入法输入中文
- 异常详细信息: System.Configuration.Provider.ProviderException: 未启用角色管理器功能。
- 在c#中实现socket编程,我们应该遵循什么原则选择异步还是同步?
- 如何实现多个应用程序对文件的读写操作????高手们来帮忙啊。。。
- 问一个WenForm中变量的生存期问题?
- 孤单单的27岁生日(1977-10-16)散分庆祝。
- 新增一个递增列后,此列数据显示不出来。
- 30岁的人来看一看--我实在没办法,就是无法保存数据vs2005---C#
- 在Winform中如何自动生成控件?
- 怎么使用WCF访问SOCKET,有例子最好,谢谢
- 新手关于安装环境的问题
你可以int j = 0;
for (int i = 0; i < BGRValues.Length; i++)
{
if (BGRValues[i] != 255) j++;
}
MessageBox.Show(j+"");看到有几万个不是255的没?
{
Bitmap bmp = new Bitmap(tbSource.Text.Trim()); // Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat); // Get the address of the first line.
IntPtr ptr = bmpData.Scan0; // Declare an array to hold the bytes of the bitmap.
int bytes = Math.Abs(bmpData.Stride) * bmp.Height;
byte[] rgbValues = new byte[bytes]; // Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
for (int i = 0; i < rgbValues.Length; i++)
{
if (rgbValues[i] != 255)
{
MessageBox.Show(i.ToString());
break;
}
}
/*
// Set every third value to 255. A 24bpp bitmap will look red.
for (int counter = 2; counter < rgbValues.Length; counter += 3)
rgbValues[counter] = 255; // Copy the RGB values back to the bitmap
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes); // Unlock the bits. // Draw the modified image.
e.Graphics.DrawImage(bmp, 0, 150);*/
bmp.UnlockBits(bmpData);
}
string fileName = Application.StartupPath.ToString() + "\\ticket.bmp";
Bitmap bmp = new Bitmap(Image.FromFile(fileName));
和
Bitmap bmp = new Bitmap(“ticket.bmp");
有区别吗?
/// <summary>
/// 缩小图片
/// </summary>
/// <param name="strOldPic">源图文件名(包括路径)</param>
/// <param name="intWidth">缩小至宽度</param>
/// <param name="intHeight">缩小至高度</param>
public static byte[] SmallPic(string strOldPic, int intWidth, int intHeight)
{ System.Drawing.Bitmap objPic, objNewPic;
try
{
objPic = new System.Drawing.Bitmap(strOldPic);
objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
System.IO.MemoryStream mstream = new System.IO.MemoryStream();
objNewPic.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg);
return mstream.ToArray();
}
catch (Exception exp) { Logger.DebugLog(exp.Message);
return new byte[0];
}
finally
{
objPic = null;
objNewPic = null;
GC.Collect();
}
}
这是我之前写的缩放,并且导出byte[] 你可以参考一下
byte[] array = new byte[] { 0x31, 0x33, 0x36, 50, 0x30, 0x3d, 0x31 };
byte[] buffer2 = new byte[0x30];
buffer2[0] = 0x35;
buffer2[1] = 50;
buffer2[2] = 0x30;
buffer2[3] = 0x34;
buffer2[4] = 0x33;
for (int i = 5; i < buffer2.Length; i++)
{
buffer2[i] = 0x30;
}
buffer2[buffer2.Length - 1] = 0x31;
int num2 = ((this.bitmap.Width * this.bitmap.Height) / 8) + 9;
byte[] sourceArray = new byte[num2];
BitmapData bitmapdata = this.bitmap.LockBits(new Rectangle(0, 0, this.bitmap.Width, this.bitmap.Height), ImageLockMode.ReadOnly, this.bitmap.PixelFormat);
IntPtr ptr1 = bitmapdata.Scan0;
int num3 = this.bitmap.Width * this.bitmap.Height;
byte num5 = 0;
byte num6 = 0;
int num7 = 9;
for (int j = 0; j < num3; j++)
{
byte num4;
if (Marshal.ReadByte(bitmapdata.Scan0, j) > 0x80)
{
num4 = 0;
}
else
{
num4 = 1;
}
if (num5 == 0)
{
num6 = 0;
}
num6 = (byte) (num6 ^ ((byte) (num4 << (7 - num5))));
num5 = (byte) (num5 + 1);
if (num5 > 7)
{
num5 = 0;
sourceArray[num7++] = (byte) (sourceArray[num7++] + Convert.ToByte(num6));
}
}
this.bitmap.UnlockBits(bitmapdata);//到这应该就是我一开始发的代码
string str = string.Format("{0:D3}", this.bitmap.Height);
sourceArray[0] = 0x37;
sourceArray[1] = 0x36;
sourceArray[2] = 0x30;
sourceArray[3] = 0x30;
sourceArray[4] = 0x30;
sourceArray[5] = 0x36;
sourceArray[6] = (byte) str[0];
sourceArray[7] = (byte) str[1];
sourceArray[8] = (byte) str[2];
Array.Resize<byte>(ref array, array.Length + sourceArray.Length);
Array.Copy(sourceArray, 0, array, array.Length - sourceArray.Length, sourceArray.Length);//到这不知道是干什么的?
KGB_WRAPPER_RESULT kgb_wrapper_result = this.device.kgb.WriteSetUmsgCmd(array);//btye数组传进来。
if (this.device.kgb.ExecSendUserMessage() != KGB_WRAPPER_RESULT.ERR_OK)
Image image = Image.FromFile("文件名");
byte[] buffer = new byte[image.Width * image.Height];
MemoryStream ms = new MemoryStream(buffer);
image.Save(ms, image.RawFormat);
ms.Close();
ms.Dispose();
image.Dispose();
Image image=Image.FromStream(ms);
//....
//...