2011-02-18 63 views
0

我有兩種方法完全相同,除了一個在位圖上,另一個在圖像上。我想只能有一種方法,因此它更乾淨,但我不知道如何實現這一點。如果不能將這兩種方法放在一起,那麼簡化和壓縮這些代碼的最好方法是什麼?將這兩種圖像處理方法合爲一體?

謝謝!

static private Bitmap resizeBitmap(Bitmap inputBitmap, Orientation orientation) { 
    double scaleX = 1; 
    double scaleY = 1; 
    int pageWidth = orientation == Orientation.Portrait ? (int)PageDimensions.Width : (int)PageDimensions.Height; 
    int pageHeight = orientation == Orientation.Portrait ? (int)PageDimensions.Height : (int)PageDimensions.Width; 
    if (inputBitmap.Width > pageWidth) { 
     scaleX = Convert.ToDouble(pageWidth)/Convert.ToDouble(inputBitmap.Width); 
     scaleY = scaleX; 
    } 
    if (inputBitmap.Height * scaleY > pageHeight) { 
     scaleY = scaleY * Convert.ToDouble(pageHeight)/Convert.ToDouble(inputBitmap.Height); 
     scaleX = scaleY; 
    } 
    Bitmap outputImage = new Bitmap(Convert.ToInt16(inputBitmap.Width * scaleX), Convert.ToInt16(inputBitmap.Height * scaleY)); 
    using (Graphics g = Graphics.FromImage(outputImage)) 
     g.DrawImage(inputBitmap, 0, 0, outputImage.Width, outputImage.Height); 
    return outputImage; 
} 
static private Image resizeImage(Image inputImage, Orientation orientation) { 
    double scaleX = 1; 
    double scaleY = 1; 
    int pageWidth = orientation == Orientation.Portrait ? (int)PageDimensions.Width : (int)PageDimensions.Height; 
    int pageHeight = orientation == Orientation.Portrait ? (int)PageDimensions.Height : (int)PageDimensions.Width; 
    if (inputImage.Width > pageWidth) { 
     scaleX = Convert.ToDouble(pageWidth)/Convert.ToDouble(inputImage.Width); 
     scaleY = scaleX; 
    } 
    if (inputImage.Height * scaleY > pageHeight) { 
     scaleY = scaleY * Convert.ToDouble(pageHeight)/Convert.ToDouble(inputImage.Height); 
     scaleX = scaleY; 
    } 
    Bitmap outputImage = new Bitmap(Convert.ToInt16(inputImage.Width * scaleX), Convert.ToInt16(inputImage.Height * scaleY)); 
    using(Graphics g = Graphics.FromImage(outputImage)) 
     g.DrawImage(inputImage, 0, 0, outputImage.Width, outputImage.Height); 
    return outputImage; 
} 

回答

1

你只需要一個單個函數採用Image參數並且返回類型爲Bitmap,因爲實際上您在resizeImage方法中返回Bitmap。此作品自Bitmap繼承Image

static private Bitmap resizeImage(Image inputImage, Orientation orientation) 
{ 
    ... 
} 

這樣您不必如果你將其分配給Bitmap類型的變量(我以爲是你寫的這兩種功能的最初原因)投resizeImage的結果。

+0

現在這就是我所希望的,我剛剛實施了你的解決方案,它的效果非常好:) – sooprise 2011-02-18 19:17:41

0

我不知道這將是非常有用的,但,你可以在這些功能中的一個,添加一個IF知道,如果你的參數的類型是位圖或圖像的對象。和你一起加入2個功能

祝你好運!

3

我不是一個C#專家,但據the documentation,位圖影像(從它繼承),所以你可以只調用了位圖作爲參數的第二種方法,它應該工作。如果我是正確的,那麼只要刪除第一個,這是沒有用的了。

1

在一般情況下,如果你在處理兩個沒有公共基類或接口的類(並且你不能添加一個),最好的辦法是把不依賴於特定的代碼類轉換爲兩個現有方法調用的新方法。例如,像:

/// getNewDimensions 
static private void getNewDimensions(int ImageWidth, int ImageHeight, Orientation orientation, out int NewWidth, out int NewHeight) { 
    double scaleX = 1; 
    double scaleY = 1; 
    int pageWidth = orientation == Orientation.Portrait ? (int)PageDimensions.Width : (int)PageDimensions.Height; 
    int pageHeight = orientation == Orientation.Portrait ? (int)PageDimensions.Height : (int)PageDimensions.Width; 
    if (ImageWidth > pageWidth) { 
     scaleX = Convert.ToDouble(pageWidth)/Convert.ToDouble(ImageWidth); 
     scaleY = scaleX; 
    } 
    if (ImageHeight * scaleY > pageHeight) { 
     scaleY = scaleY * Convert.ToDouble(pageHeight)/Convert.ToDouble(ImageHeight); 
     scaleX = scaleY; 
    } 

    NewWidth = ImageWidth * scaleX; 
    NewHeight = ImageHeight * scaleY; 

} 

/// resizeBitmap 
static private Bitmap resizeBitmap(Bitmap inputBitmap, Orientation orientation) { 
    int NewWidth = 0; 
    int NewHeight = 0; 
    getNewDimensions(inputBitmap.Width, inputBitmap.Width, orientation, NewWidth, NewHeight); 
    Bitmap outputImage = new Bitmap(Convert.ToInt16(newWidth), Convert.ToInt16(newHeight)); 
    using (Graphics g = Graphics.FromImage(outputImage)) 
     g.DrawImage(inputBitmap, 0, 0, outputImage.Width, outputImage.Height); 
    return outputImage; 
} 

/// resizeImage (I'll leave as an exercise to the reader) 
+0

這是我最終會做的,如果沒有更好的選擇,但不應該有一種方法來簡單地將圖像轉換爲位圖,如果這是輸入圖像類型? – sooprise 2011-02-18 19:06:33

0

我不喜歡張貼的答案我自己的問題,但是這似乎是相當不錯的了,是一個簡單的解決方案:

static private Bitmap resizeBitmap(Bitmap inputBitmap, Orientation orientation) { 
    return (Bitmap)resizeImage(inputBitmap, orientation); 
} 

//Resizes images 
static private Image resizeImage(Image inputImage, Orientation orientation) { 
    double scaleX = 1; 
    double scaleY = 1; 
    int pageWidth = orientation == Orientation.Portrait ? (int)PageDimensions.Width : (int)PageDimensions.Height; 
    int pageHeight = orientation == Orientation.Portrait ? (int)PageDimensions.Height : (int)PageDimensions.Width; 
    if (inputImage.Width > pageWidth) { 
     scaleX = Convert.ToDouble(pageWidth)/Convert.ToDouble(inputImage.Width); 
     scaleY = scaleX; 
    } 
    if (inputImage.Height * scaleY > pageHeight) { 
     scaleY = scaleY * Convert.ToDouble(pageHeight)/Convert.ToDouble(inputImage.Height); 
     scaleX = scaleY; 
    } 
    Bitmap outputImage = new Bitmap(Convert.ToInt16(inputImage.Width * scaleX), Convert.ToInt16(inputImage.Height * scaleY)); 
    using (Graphics g = Graphics.FromImage(outputImage)) 
     g.DrawImage(inputImage, 0, 0, outputImage.Width, outputImage.Height); 
    return outputImage; 
} 
相關問題