熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

.Net圖片處理函數

2022-06-13   來源: .NET編程 

  /// <summary>
/// 獲取一個圖片按等比例縮小後的大小
/// </summary>
/// <param name="maxWidth">需要縮小到的寬度</param>
/// <param name="maxHeight">需要縮小到的高度</param>
/// <param name="imageOriginalWidth">圖片的原始寬度</param>
/// <param name="imageOriginalHeight">圖片的原始高度</param>
/// <returns>返回圖片按等比例縮小後的實際大小</returns>
public static Size GetNewSize(int maxWidth int maxHeight int imageOriginalWidth int imageOriginalHeight)
{
    double w = ;
    double h = ;
    double sw = ConvertToDouble(imageOriginalWidth);
    double sh = ConvertToDouble(imageOriginalHeight);
    double mw = ConvertToDouble(maxWidth);
    double mh = ConvertToDouble(maxHeight);

    if (sw < mw && sh < mh)
    {
        w = sw;
        h = sh;
    }
    else if ((sw / sh) > (mw / mh))
    {
        w = maxWidth;
        h = (w * sh) / sw;
    }
    else
    {
        h = maxHeight;
        w = (h * sw) / sh;
    }

    return new Size(ConvertToInt(w) ConvertToInt(h));
}

/// <summary>
/// 對給定的一個圖片(Image對象)生成一個指定大小的縮略圖
/// </summary>
/// <param name="originalImage">原始圖片</param>
/// <param name="thumMaxWidth">縮略圖的寬度</param>
/// <param name="thumMaxHeight">縮略圖的高度</param>
/// <returns>返回縮略圖的Image對象</returns>
public static SystemDrawingImage GetThumbNailImage(SystemDrawingImage originalImage int thumMaxWidth int thumMaxHeight)
{
    Size thumRealSize = SizeEmpty;
    SystemDrawingImage newImage = originalImage;
    Graphics graphics = null;

    try
    {
        thumRealSize = GetNewSize(thumMaxWidth thumMaxHeight originalImageWidth originalImageHeight);
        newImage = new Bitmap(thumRealSizeWidth thumRealSizeHeight);
        graphics = GraphicsFromImage(newImage);

        graphicsCompositingQuality = CompositingQualityHighQuality;
        graphicsInterpolationMode = InterpolationModeHighQualityBicubic;
        graphicsSmoothingMode = SmoothingModeHighQuality;

        graphicsClear(ColorTransparent);

        graphicsDrawImage(originalImage new Rectangle(  thumRealSizeWidth thumRealSizeHeight) new Rectangle(  originalImageWidth originalImageHeight) GraphicsUnitPixel);
    }
    catch { }
    finally
    {
        if (graphics != null)
        {
            graphicsDispose();
            graphics = null;
        }
    }

    return newImage;
}
/// <summary>
/// 對給定的一個圖片文件生成一個指定大小的縮略圖
/// </summary>
/// <param name="originalImage">圖片的物理文件地址</param>
/// <param name="thumMaxWidth">縮略圖的寬度</param>
/// <param name="thumMaxHeight">縮略圖的高度</param>
/// <returns>返回縮略圖的Image對象</returns>
public static SystemDrawingImage GetThumbNailImage(string imageFile int thumMaxWidth int thumMaxHeight)
{
    SystemDrawingImage originalImage = null;
    SystemDrawingImage newImage = null;

    try
    {
        originalImage = SystemDrawingImageFromFile(imageFile);
        newImage = GetThumbNailImage(originalImage thumMaxWidth thumMaxHeight);
    }
    catch { }
    finally
    {
        if (originalImage != null)
        {
            originalImageDispose();
            originalImage = null;
        }
    }

    return newImage;
}
/// <summary>
/// 對給定的一個圖片文件生成一個指定大小的縮略圖並將縮略圖保存到指定位置
/// </summary>
/// <param name="originalImageFile">圖片的物理文件地址</param>
/// <param name="thumbNailImageFile">縮略圖的物理文件地址</param>
/// <param name="thumMaxWidth">縮略圖的寬度</param>
/// <param name="thumMaxHeight">縮略圖的高度</param>
public static void MakeThumbNail(string originalImageFile string thumbNailImageFile int thumMaxWidth int thumMaxHeight)
{
    SystemDrawingImage newImage = GetThumbNailImage(originalImageFile thumMaxWidth thumMaxHeight);
    try
    {
        newImageSave(thumbNailImageFile ImageFormatJpeg);
    }
    catch
    { }
    finally
    {
        newImageDispose();
        newImage = null;
    }
}
/// <summary>
/// 將一個圖片的內存流調整為指定大小並返回調整後的內存流
/// </summary>
/// <param name="originalImageStream">原始圖片的內存流</param>
/// <param name="newWidth">新圖片的寬度</param>
/// <param name="newHeight">新圖片的高度</param>
/// <returns>返回調整後的圖片的內存流</returns>
public static MemoryStream ResizeImage(Stream originalImageStream int newWidth int newHeight)
{
    MemoryStream newImageStream = null;

    SystemDrawingImage newImage = GlobalsGetThumbNailImage(SystemDrawingImageFromStream(originalImageStream) newWidth newHeight);
    if (newImage != null)
    {
        newImageStream = new MemoryStream();
        newImageSave(newImageStream ImageFormatJpeg);
    }

    return newImageStream;
}
/// <summary>
/// 將一個內存流保存為磁盤文件
/// </summary>
/// <param name="stream">內存流</param>
/// <param name="newFile">目標磁盤文件地址</param>
public static void SaveStreamToFile(Stream stream string newFile)
{
    if (stream == null || streamLength ==  || stringIsNullOrEmpty(newFile))
    {
        return;
    }

    byte[] buffer = new byte[streamLength];
    streamPosition = ;
    streamRead(buffer  bufferLength);
    FileStream fileStream = new FileStream(newFile FileModeOpenOrCreate FileAccessWrite);
    fileStreamWrite(buffer  bufferLength);
    fileStreamFlush();
    fileStreamClose();
    fileStreamDispose();
}
/// <summary>
/// 對一個指定的圖片加上圖片水印效果
/// </summary>
/// <param name="imageFile">圖片文件地址</param>
/// <param name="waterImage">水印圖片(Image對象)</param>
public static void CreateImageWaterMark(string imageFile SystemDrawingImage waterImage)
{
    if (stringIsNullOrEmpty(imageFile) || !FileExists(imageFile) || waterImage == null)
    {
        return;
    }

    SystemDrawingImage originalImage = SystemDrawingImageFromFile(imageFile);

    if (originalImageWidth   < waterImageWidth || originalImageHeight   < waterImageHeight)
    {
        return;
    }

    Graphics graphics = GraphicsFromImage(originalImage);

    int x = originalImageWidth  waterImageWidth  ;
    int y = originalImageHeight  waterImageHeight  ;
    int width = waterImageWidth;
    int height = waterImageHeight;

    graphicsDrawImage(waterImage new Rectangle(x y width height)   width height GraphicsUnitPixel);
    graphicsDispose();

    MemoryStream stream = new MemoryStream();
    originalImageSave(stream ImageFormatJpeg);
    originalImageDispose();

    SystemDrawingImage imageWithWater = SystemDrawingImageFromStream(stream);

    imageWithWaterSave(imageFile);
    imageWithWaterDispose();
}
/// <summary>
/// 對一個指定的圖片加上文字水印效果
/// </summary>
/// <param name="imageFile">圖片文件地址</param>
/// <param name="waterText">水印文字內容</param>
public static void CreateTextWaterMark(string imageFile string waterText)
{
    if (stringIsNullOrEmpty(imageFile) || stringIsNullOrEmpty(waterText) || !FileExists(imageFile))
    {
        return;
    }

    SystemDrawingImage originalImage = SystemDrawingImageFromFile(imageFile);

    Graphics graphics = GraphicsFromImage(originalImage);

    graphicsSmoothingMode = SmoothingModeHighQuality;
    graphicsTextRenderingHint = TextRenderingHintClearTypeGridFit;
    graphicsCompositingQuality = CompositingQualityHighQuality;
    graphicsInterpolationMode = InterpolationModeHighQualityBicubic;

    SolidBrush brush = new SolidBrush(ColorFromArgb(   ));
    Font waterTextFont = new Font("Arial"  FontStyleRegular);
    SizeF waterTextSize = graphicsMeasureString(waterText waterTextFont);

    float x = (float)originalImageWidth  waterTextSizeWidth  F;
    float y = (float)originalImageHeight  waterTextSizeHeight  F;

    graphicsDrawString(waterText waterTextFont brush x y);

    graphicsDispose();
    brushDispose();

    MemoryStream stream = new MemoryStream();
    originalImageSave(stream ImageFormatJpeg);
    originalImageDispose();

    SystemDrawingImage imageWithWater = SystemDrawingImageFromStream(stream);

    imageWithWaterSave(imageFile);
    imageWithWaterDispose();
}

/// <summary>
/// 判斷上傳組件是否包含內容
/// </summary>
/// <param name="fileUpload">ASPNET 標准上傳組件</param>
/// <returns>如果數據有效則返回True否則返回False</returns>
public static bool IsAttachmentValid(FileUpload fileUpload)
{
    if (fileUpload != null &&
        fileUploadPostedFile != null &&
        !stringIsNullOrEmpty(fileUploadPostedFileFileName) &&
        fileUploadPostedFileContentLength > )
    {
        return true;
    }
    return false;
}


From:http://tw.wingwit.com/Article/program/net/201311/14084.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.