像MS Paint一样调整位图大小

3

我需要像MS Paint中的调整大小一样调整bmp大小,也就是没有抗锯齿。 有人知道如何在c#或vb.net中实现这一点吗?


1
WPF的新功能通常比旧的System.Drawing功能更快、更好。请查看https://dev59.com/M0fRa4cB1Zd3GeqP7Ttr。 - Mikael Svenson
5个回答

4

您可以将图形插值模式设置为最近邻,然后使用drawimage进行调整大小而不进行抗锯齿处理。

Dim img As Image = Image.FromFile("c:\jpg\1.jpg")
Dim g As Graphics

pic1.Image = New Bitmap(180, 180, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
g = Graphics.FromImage(pic1.Image)
g.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
g.DrawImage(img, 0, 0, pic1.Image.Width, pic1.Image.Height)

2


1

如何从MSDN复制图像

Paint只是简单的截取图像,不是吗?那个页面上的示例有你所需的工具。


0
    // ********************************************** ScaleBitmap

    /// <summary>
    /// Scale a bitmap by a scale factor, growing or shrinking 
    /// both axes, maintaining the aspect ratio
    /// </summary>
    /// <param name="inputBmp">
    /// Bitmap to scale
    /// </param>
    /// <param name="scale_factor">
    /// Factor by which to scale
    /// </param>
    /// <returns>
    /// New bitmap containing the original image, scaled by the 
    /// scale factor
    /// </returns>
    /// <citation>
    /// A Bitmap Manipulation Class With Support For Format 
    /// Conversion, Bitmap Retrieval from a URL, Overlays, etc.,
    /// Adam Nelson, The Code Project, September 2003.
    /// </citation>

    private Bitmap ScaleBitmap ( Bitmap  bitmap,
                                 float   scale_factor )
        {
        Graphics    g = null;
        Bitmap      new_bitmap = null;
        Rectangle   rectangle;

        int  height = ( int ) ( ( float ) bitmap.Size.Height *
                                scale_factor );
        int  width = ( int ) ( ( float ) bitmap.Size.Width *
                               scale_factor );
        new_bitmap = new Bitmap ( width,
                                  height,
                                  PixelFormat.Format24bppRgb );

        g = Graphics.FromImage ( ( Image ) new_bitmap );
        g.InterpolationMode = InterpolationMode.High;
        g.ScaleTransform ( scale_factor, scale_factor );

        rectangle = new Rectangle ( 0,
                                    0,
                                    bitmap.Size.Width,
                                    bitmap.Size.Height );
        g.DrawImage ( bitmap,
                      rectangle,
                      rectangle,
                      GraphicsUnit.Pixel );
        g.Dispose ( );

        return ( new_bitmap );
        }

你需要使用InterpolationMode.NearestNeighbor,而不是InterpolationMode.High。 - BrainSlugs83

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接