Мне нужно, чтобы мое масштабированное изображение было псевдонимом в C #
Вопрос
Это может быть странный вопрос, но когда я масштабирую свое изображение в C #, мне нужно, чтобы оно было пикселированным, а не сглаженным. Так же, как в MSpaint, когда вы масштабируете.
Я надеюсь, что сглаживание изображений по умолчанию в C #, иначе я изменил что-то, чего не хотел.
Я пытался поиграться с Graphics.InterpolationMode
, но не повезло. Я использую объект Bitmap для хранения изображения, и оно создается следующим образом:
// A custom control holds the image
this.m_ZoomPanPicBox.Image = new Bitmap(szImagePath);
И краткий синапсис пользовательского элемента управления:
class ZoomPanPicBox : ScrollableControl
{
Image m_image;
float m_zoom = 1.0f;
InterpolationMode m_interpolationMode;
...
////////////////////////////////////////////////////////
public ZoomPanPicBox()
{
//Double buffer the control
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
this.AutoScroll=true;
}
////////////////////////////////////////////////////////
protected override void OnPaint(PaintEventArgs e)
{
//if no image, don't bother
if(m_image==null)
{
base.OnPaintBackground(e);
return;
}
//Set up a zoom matrix
Matrix mx = new Matrix(m_zoom,0,0,m_zoom,0,0);
//now translate the matrix into position for the scrollbars
mx.Translate(this.AutoScrollPosition.X / m_zoom, this.AutoScrollPosition.Y / m_zoom);
//use the transform
e.Graphics.Transform = mx;
//and the desired interpolation mode
e.Graphics.InterpolationMode = m_interpolationMode;
//Draw the image ignoring the images resolution settings.
e.Graphics.DrawImage(m_image,new Rectangle(0,0,this.m_image.Width,this.m_image.Height),0,0,m_image.Width, m_image.Height,GraphicsUnit.Pixel);
base.OnPaint(e);
}
Есть идеи? Спасибо.
Решение
На самом деле, вы правы в InterpolationMode, как как говорят в документах а>. Просто установите для него значение InterpolationMode.NearestNeighbor. В вашем примере кода вы никогда не устанавливаете m_interpolationMode.
Другие советы
Ну, вы могли бы реализовать масштаб самостоятельно и выполнить простую линейную интерполяцию (т.е. не делайте усреднение соседей, например, бикубическое) ... Это выглядит красиво и блочно.