Wednesday, June 28, 2006

Một vài lưu ý khi làm với Image trên .Net (cont)

3. Điều chỉnh các thông số trước khi save : dùng để điều chỉnh Quality của ảnh, v.v.... giúp giảm size của ảnh

Listing 1: Rotating a Bitmap Image

private int _totalRotation = 0;


private void btnRotateCW_Click
(object sender, System.EventArgs e)
{
// Rotate image clockwise (90)
if (pbox.Image != null)
{
_totalRotation += 90;
pbox.Image.RotateFlip(
RotateFlipType.Rotate90FlipNone);
pbox.Invalidate();
}
}


private void btnRotateCC_Click
(object sender, System.EventArgs e)
{
// Rotate image counter-clockwise (270)
if (pbox.Image != null)
{
_totalRotation -= 90;
pbox.Image.RotateFlip(
RotateFlipType.Rotate270FlipNone);
pbox.Invalidate();
}
}



Listing 2: Saving a Bitmap Image

private string _origFileName = null;


private void btnSave_Click
(object sender, EventArgs e)
{
if (_origFileName == null)
return;


SaveFileDialog dlg = new SaveFileDialog();


dlg.Filter = "JPG Files (*.jpg)|*.jpg";
dlg.FileName = _origFileName;


if (dlg.ShowDialog() == DialogResult.OK)
{
pbox.Image.Save(dlg.FileName,
ImageFormat.Jpeg);
}
}



Listing 3: Locating an ImageCodecInfo object

private ImageCodecInfo GetImageCodec(string mimeType)
{
ImageCodecInfo[] codecs
= ImageCodecInfo.GetImageEncoders();


foreach (ImageCodecInfo codec in codecs)
{
if (String.Compare(codec.MimeType,
mimeType, true) == 0)
{
return codec;
}
}


return null;
}






Listing 4: Saving an image using encoder parameters


private string _origFileName = null;
private Bitmap _origBitmap = null;
private ImageCodecInfo jpgCodec = null;


private void btnSave_Click
(object sender, EventArgs e)
{
if (_origFileName == null)
return;


SaveFileDialog dlg = new SaveFileDialog();


dlg.Filter = "JPG Files (*.jpg)|*.jpg";
dlg.FileName = _origFileName;


if (dlg.ShowDialog() != DialogResult.OK)
return;


// Ready to save the image
while (_totalRotation < 0)
_totalRotation += 360;
_totalRotation %= 360;
if (_totalRotation == 0 && !cboxQuality.Checked)
{
// No parameters, so just save image.
_origBitmap.Save(dlg.FileName,
ImageFormat.Jpeg);
}
else
{
if (jpgCodec == null)
jpgCodec = GetImageCodec("image/jpeg");


// Determine required number of parameters
int paramSize = udQuality.Enabled ? 1 : 0;
if (_totalRotation != 0)
paramSize ++;
EncoderParameters encoderParams
= new EncoderParameters(paramSize);


if (_totalRotation != 0)
{
// Create transformation parameter
EncoderValue rValue
= EncoderValue.TransformRotate90;
if (_totalRotation == 180)
rValue = EncoderValue.TransformRotate180;
else if (_totalRotation == 270)
rValue = EncoderValue.TransformRotate270;


EncoderParameter tParam
= new EncoderParameter(
Encoder.Transformation,
(long)rotateValue);
encoderParams.Param[paramSize-1] = tParam;
}


if (udQuality.Enabled)
{
// Create quality parameter
EncoderParameter qParam
= new EncoderParameter(
Encoder.Quality,
(long)udQuality.Value);
encoderParams.Param[0] = qParam;
}


// Save the image using encoder settings
_origBitmap.Save(dlg.FileName,
jpgCodec, encoderParams);
}
}

No comments: