2009-10-26 12 views
6

मैं इस बार कुछ बार आया हूं और सोचा कि इसे वहां रखना अच्छा होगा। आपकी सबसे अच्छी छवि का आकार और/या फसल तर्क क्या है। विचार यह है कि कुछ विधि को लक्षित छवि, आयाम और फसल ध्वज के साथ बुलाया जाता है - यह वापस या सहेजने या इच्छित छवि को सहेज लेगा।सर्वश्रेष्ठ आकार बदलें या फसल तर्क

मेरा नीचे है। वीबी से सी # में कनवर्ट किया गया है, इसलिए हाँ छोटी छोटी चीजें होंगी लेकिन तर्क वह है जिसे हम देख रहे हैं।

// INIT 
// On/off 
bool WeAreCropping = true; 

// Get some dimensions 
int TargetWidth = RequestedWidth; 
int TargetHeight = RequestedHeight; 
int SourceWidth = SourceImage.Width; 
int SourceHeight = SourceImage.Height; 
int ResizeWidth = TargetWidth; 
int ResizeHeight = TargetHeight; 

// GET RESIZE VALUES 
// Are we cropping? 
if (WeAreCropping) { 

    // Get source and target aspect ratio 
    double SourceAspectRatio = SourceWidth/SourceHeight; 
    double TargetAspectRatio = TargetWidth/TargetHeight; 

    // Compare aspect ratios to find out if we should we resize by 
    // width or height or nothing 
    if (TargetAspectRatio < SourceAspectRatio) { 
     ResizeWidth = TargetHeight/SourceHeight * SourceWidth; 
    } 
    else if (TargetAspectRatio > SourceAspectRatio) { 
     ResizeHeight = TargetWidth/SourceWidth * SourceHeight; 
    } 
    else { 
     // Same aspect ratio 
    } 


} 
else { 

    // If the target image is bigger than the source 
    if (TargetWidth > SourceWidth && TargetHeight > SourceHeight) { 
     TargetWidth = SourceWidth; 
     TargetHeight = SourceHeight; 
    } 

    double Ratio = 0; 

    // What ratio should we resize it by 
    if (SourceWidth/TargetWidth > SourceHeight/TargetHeight) { 
     Ratio = SourceWidth/TargetWidth; 
    } 
    else { 
     Ratio = SourceHeight/TargetHeight; 
    } 

    ResizeWidth = Math.Ceiling(SourceWidth/Ratio); 

    ResizeHeight = Math.Ceiling(SourceHeight/Ratio); 
} 

// TIME TO DO SUMFINK 
// Resize the image using ResizeWidth and ResizeHeight 
// Do it 

if (WeAreCropping) { 
    // Crop the resized image at the center TargetWidth and TargetHeight 
    // Do it 
} 

मुझे संदेह है कि यह अधिक सुरुचिपूर्ण हो सकता है ताकि आप बेहतर कर सकें।

उत्तर

4

मैं कहूंगा कि आपको मानक ज्यामिति प्रकारों का उपयोग करना चाहिए जैसे Rectangle और Size। तो आपको शायद एक केस केस का भी समर्थन करना चाहिए, जब कॉलर कुछ बड़े आयत में छवि को फिट करना चाहता है, लेकिन फिर भी मूल छवि आकार अनुपात रखना चाहता है।

आकार बदलने के लिए एक तरीका आप here पा सकते हैं।

+0

लिंक के लिए धन्यवाद। – Maleks

+9

"धन्यवाद" के लिए एक विशेष बटन है :)) –

+0

लिंक टूटा हुआ है – Flexicoder

0

यह शायद आपको जो चाहिए उसे तुलना में थोड़ा अधिक शामिल है, लेकिन कुछ उन्नत फसल तकनीकों, check this out के उदाहरण के लिए।

+0

लिंक मुझे 500 त्रुटि देता है। –

संबंधित मुद्दे