C# 如何更改位图的大小;使用;块

C# 如何更改位图的大小;使用;块,c#,C#,在下面的代码中,我在bitmap=newBitmap行上得到一个错误,说它不能分配位图,因为它是一个变量。如何调整位图的大小 public static Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight) { Bitmap result = new Bitmap(nWidth, nHeight); using (Graphics g = Graphics.FromImage((Image)r

在下面的代码中,我在
bitmap=newBitmap
行上得到一个错误,说它不能分配位图,因为它是一个
变量。如何调整位图的大小

public static Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
{
            Bitmap result = new Bitmap(nWidth, nHeight);
            using (Graphics g = Graphics.FromImage((Image)result))
                g.DrawImage(b, 0, 0, nWidth, nHeight);
            return result;
}

....

using (var bitmap = new Bitmap(_width, _height, _width * 3, PixelFormat.Format24bppRgb, pBuffer))
{
    if (!this.Secondpass)
    {
        long[] HistogramValues = Form1.GetHistogram(bitmap);
        Form1.Histograms.Add(HistogramValues);
        long t = Form1.GetTopLumAmount(HistogramValues, 1000);
        Form1.averagesTest.Add(t);                            
    }
    else
    {                    
        if (_frameId > 0)
        {
            double t = Form1.averagesTest[_frameId] / 1000.0 - Form1.averagesTest[_frameId - 1] / 1000.0;
            w.WriteLine("averagesTest >>>  " + t);
            double tt = framesCounts();
            if (_frameId == framesCounts())
            {
                w.Close();
            }
            if (Form1.averagesTest[_frameId] / 1000.0 - Form1.averagesTest[_frameId - 1] / 1000.0 > 0.0) 
            {
                count = 6;
            }

            if (count > 0)
            {
                Bitmap newBitmap = ResizeBitmap(bitmap, 10, 10);
                bitmap.Dispose();
                bitmap = newBitmap;
                bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
                bitmap.Save(Path.Combine(_outFolder, _frameId.ToString("D6") + ".bmp"),ImageFormat.Bmp);
                count --;
            }
        }
    }
}

您正在尝试使用
语句重新分配在
中定义的变量
bitmap
,这是一个错误:

资源获取中声明的局部变量是只读的,并且必须包含初始值设定项。如果嵌入式语句试图修改这些局部变量(通过赋值或++和-)或将它们作为ref或out参数传递,则会发生编译时错误


也许还有别的问题,但你为什么还要用这句话呢?。newbitmap.RotateFlip和newbitmap.Save。不管怎样,我还是很困惑。。