PDA

View Full Version : کاهش کیفیت ترسیم روی تصویر



ali_72
دوشنبه 01 تیر 1394, 08:52 صبح
سلام
از کد زیر برای نمایش بزرگنماپی روی تصویر استفاده کردم
که یک خط نیز روی تصویر ترسیم میشه
زمانی که ضریب بزرگنماپی را افزایش میدم
خطی که روش ترسیم میکنم بی کیفیت میشه
ممنون میشم راهنمایی کنید


/// <summary> /// تنظیم مقدار بزرگنمائی در ترک بار
/// </summary>
private void trbZoomFactor_ValueChanged(object sender, EventArgs e)
{
_zoomFactor = trbZoomFactor.Value;
_lbl_zoom.Text = string.Format("x{0}", _zoomFactor);
}


private void _pic_image_MouseMove(object sender, MouseEventArgs e)
{
if (_pic_image.Image == null)
return;


_originalImage = _pic_image.Image;
ResizeAndDisplayImage();
UpdateZoomedImage(e);
}


/// <summary>
/// آپدیت تصویر پیکچرباکس بزرگنمائی
/// </summary>
private void UpdateZoomedImage(MouseEventArgs e)
{
int zoomWidth = picZoom.Width / _zoomFactor;
int zoomHeight = picZoom.Height / _zoomFactor;


int halfWidth = zoomWidth / 2;
int halfHeight = zoomHeight / 2;


Bitmap tempBitmap = new Bitmap(zoomWidth, zoomHeight, PixelFormat.Format24bppRgb);


Graphics bmGraphics = Graphics.FromImage(tempBitmap);


bmGraphics.Clear(_pic_image.BackColor);


bmGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;


bmGraphics.DrawImage(_pic_image.Image,
new Rectangle(0, 0, zoomWidth, zoomHeight),
new Rectangle(e.X - halfWidth, e.Y - halfHeight, zoomWidth, zoomHeight),
GraphicsUnit.Pixel);


picZoom.Image = tempBitmap;


bmGraphics.DrawLine(Pens.Black, halfWidth + 1, halfHeight - 4, halfWidth + 1, halfHeight - 1);



bmGraphics.Dispose();


picZoom.Refresh();
}


private Image _originalImage;
/// <summary>
/// تغییر سایز و نمایش تصویر بزرگنمائی
/// </summary>
private void ResizeAndDisplayImage()
{
if (_originalImage == null)
return;


_pic_image.Image = _originalImage;

int sourceWidth = _originalImage.Width;
int sourceHeight = _originalImage.Height;
int targetWidth;
int targetHeight;
double ratio;


if (sourceWidth > sourceHeight)
{
targetWidth = _pic_image.Width;

ratio = (double)targetWidth / sourceWidth;

targetHeight = (int)(ratio * sourceHeight);
}
else if (sourceWidth < sourceHeight)
{
targetHeight = _pic_image.Height;

ratio = (double)targetHeight / sourceHeight;

targetWidth = (int)(ratio * sourceWidth);
}
else
{
targetHeight = _pic_image.Height;
targetWidth = _pic_image.Width;
}


int targetTop = (_pic_image.Height - targetHeight) / 2;
int targetLeft = (_pic_image.Width - targetWidth) / 2;


Bitmap tempBitmap = new Bitmap(_pic_image.Width, _pic_image.Height, PixelFormat.Format24bppRgb);



tempBitmap.SetResolution(_originalImage.Horizontal Resolution, _originalImage.VerticalResolution);

Graphics bmGraphics = Graphics.FromImage(tempBitmap);


// First clear the image with the current backcolor
bmGraphics.Clear(_pic_image.BackColor);


// Set the interpolationmode since we are resizing an image here
bmGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;


// Draw the original image on the temporary bitmap, resizing it using
// the calculated values of targetWidth and targetHeight.
bmGraphics.DrawImage(_originalImage,
new Rectangle(targetLeft, targetTop, targetWidth, targetHeight),
new Rectangle(0, 0, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);


// Dispose of the bmGraphics object
bmGraphics.Dispose();


_pic_image.Image = tempBitmap;
}



private void picZoom_MouseHover(object sender, EventArgs e)
{
trbZoomFactor.Focus();
}