try
{
Bitmap bm = new Bitmap(pics[ip]);
BitmapToBytes(bm).Reverse().Take(2);
}
catch (Exception ex)
{
string imgName = Path.GetFileName(pics[ip]);
File.Move(pics[ip], txtImgDir.Text + "\\badimg\\" + imgName);
}
public
static
byte
[] BitmapToBytes(Bitmap Bitmap)
{
try
{
using
(MemoryStream ms =
new
MemoryStream())
{
Bitmap.Save(ms, Bitmap.RawFormat);
byte
[] byteImage =
new
Byte[ms.Length];
byteImage = ms.ToArray();
return
byteImage;
}
}
finally
{
}
}
BitmapToBytes(img).Reverse().Take(2);
//然后判斷是不是217,255
|
以二進制文件打開文件. 讀取最后2個字節判斷一下.
或者讀取全部,從結尾處開始查找ffd9,找到了就表示OK.
有時候用相機網服務器傳圖片的時候,突然斷網,造成圖片不完整,都是上半部分是圖片,線板部分,不能預覽。怎樣用C#判斷圖片(jpg)是否完整
C#中Bitmap類實現對圖像操作的一些方法
導入以下兩個包:
System.Drawing;
System.Drawing.Imaging;
建產對象:
Bitmap bm = new Bitmap("c:/1.bmp");
縮放:
Bitmap bm1 = new Bitmap(bm,width,height);
格式轉換:
bm.save("c:/1.jpg",ImageFromat.Jpeg);
bm1.Save("c:/1.gif", ImageFormat.Gif);
剪切一個區域:
//剪切大小
int cutwidth;
int cutheight;
Graphics g;
//以大小為剪切大小,像素格式為32位RGB創建一個位圖對像
Bitmap bm1 = new Bitmap(width,height,PixelFormat.Format32bppRgb) ;
//定義一個區域
Rectangle rg = new Rectangle(0,0,cutwidth,cutheight);
//要繪制到的位圖
g = Graphics.FromImage(bm1);
//將bm內rg所指定的區域繪制到bm1
g.DrawImage(bm,rg)
============================================
C#Bitmap代替另一個Bitmap的某部分
Bitmap bm = new Bitmap(寬度, 高度);// 新建一個 Bitmap 位圖
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm); // 根據新建的 Bitmap 位圖,創建畫布
g.Clear(System.Drawing.Color.Black);// 使用黑色重置畫布
g.DrawImage(源位圖, ......); // 繪制“源位圖”,后面有若干參數控制大小、坐標等等功能。
Bitmap bm = new Bitmap(寬度, 高度);// 新建一個 Bitmap 位圖
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm); // 根據新建的 Bitmap 位圖,創建畫布
g.Clear(System.Drawing.Color.Black);// 使用黑色重置畫布
g.DrawImage(源位圖, ......); // 繪制“源位圖”,后面有若干參數控制大小、坐標等等功能。
==================================================
C# 圖片處理之:旋轉圖片任意角度
C# 圖片處理之:旋轉圖片任意角度

/// <summary>
///
任意角度旋轉
/// </summary>
/// <param name="bmp">原始圖Bitmap</param>
/// <param name="angle">旋轉角度</param>
/// <param name="bkColor">背景色</param>
/// <returns>輸出Bitmap</returns>
public static Bitmap KiRotate(Bitmap bmp, float
angle, Color bkColor)
...
{
int w = bmp.Width + 2
;
int h = bmp.Height + 2
;
PixelFormat pf;
if (bkColor ==
Color.Transparent)
...
{
pf =
PixelFormat.Format32bppArgb;
}
else
...
{
pf =
bmp.PixelFormat;
}
Bitmap tmp
= new
Bitmap(w, h, pf);
Graphics g =
Graphics.FromImage(tmp);
g.Clear(bkColor);
g.DrawImageUnscaled(bmp, 1, 1
);
g.Dispose();
GraphicsPath path = new
GraphicsPath();
path.AddRectangle(new
RectangleF(0f, 0f, w, h));
Matrix mtrx = new
Matrix();
mtrx.Rotate(angle);
RectangleF rct =
path.GetBounds(mtrx);
Bitmap dst = new Bitmap((int)rct.Width, (int
)rct.Height, pf);
g =
Graphics.FromImage(dst);
g.Clear(bkColor);
g.TranslateTransform(-rct.X, -
rct.Y);
g.RotateTransform(angle);
g.InterpolationMode =
InterpolationMode.HighQualityBilinear;
g.DrawImageUnscaled(tmp, 0, 0
);
g.Dispose();
tmp.Dispose();
return
dst;
}
