zoukankan      html  css  js  c++  java
  • C#中Bitmap类 对图像の操作 可检测图片完整性

    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(源位图, ......); // 绘制“源位图”,后面有若干参数控制大小、坐标等等功能。
    ==================================================
    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;
    }
  • 相关阅读:
    【Codeforces 349B】Color the Fence
    【Codeforces 459D】Pashmak and Parmida's problem
    【Codeforces 467C】George and Job
    【Codeforces 161D】Distance in Tree
    【Codeforces 522A】Reposts
    【Codeforces 225C】Barcode
    【Codeforces 446A】DZY Loves Sequences
    【Codeforces 429B】Working out
    【Codeforces 478C】Table Decorations
    【Codeforces 478C】Table Decorations
  • 原文地址:https://www.cnblogs.com/alex-13/p/4843465.html
Copyright © 2011-2022 走看看