Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 缩放或调整8位图像的大小,结果为8位图像(PixelFormat.Format8Bppined)_C#_.net_Image_Resize_8 Bit - Fatal编程技术网

C# 缩放或调整8位图像的大小,结果为8位图像(PixelFormat.Format8Bppined)

C# 缩放或调整8位图像的大小,结果为8位图像(PixelFormat.Format8Bppined),c#,.net,image,resize,8-bit,C#,.net,Image,Resize,8 Bit,我尝试在c#/.NET中缩放一个灰色8图像,以便通过TCP客户端进行视频传输。 我的IpCam图像当前为1920x1080,我想将其调整为640x480,以减少连接流量: 我在stackoverflow上找到了一个工作示例,唯一的问题是 public static Image ResizeImage(Bitmap image, Size size, bool preserveAspectRatio = true) { int newWidth; int

我尝试在c#/.NET中缩放一个灰色8图像,以便通过TCP客户端进行视频传输。 我的IpCam图像当前为1920x1080,我想将其调整为640x480,以减少连接流量:

我在stackoverflow上找到了一个工作示例,唯一的问题是

 public static Image ResizeImage(Bitmap image, Size size, bool preserveAspectRatio = true)
    {
        int newWidth;
        int newHeight;
        if (preserveAspectRatio)
        {
            int originalWidth = image.Width;
            int originalHeight = image.Height;
            float percentWidth = (float)size.Width / (float)originalWidth;
            float percentHeight = (float)size.Height / (float)originalHeight;
            float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
            newWidth = (int)(originalWidth * percent);
            newHeight = (int)(originalHeight * percent);
        }
        else
        {
            newWidth = size.Width;
            newHeight = size.Height;
        }
        Image newImage = new Bitmap(newWidth, newHeight);
        using (Graphics graphicsHandle = Graphics.FromImage(newImage))
        {
            graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic; //NearestNeighbor
            graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
        }

        return(newImage);
    }
public static Image ResizeImage(位图图像,大小,bool preserveAspectRatio=true)
{
int newWidth;
int newHeight;
如果(保留Aspectratio)
{
int originalWidth=image.Width;
int originalHeight=image.Height;
浮动百分比宽度=(浮动)size.Width/(浮动)原始宽度;
浮动百分比高度=(浮动)大小.高度/(浮动)原始高度;
浮动百分比=百分比高度<百分比宽度?百分比高度:百分比宽度;
新宽度=(整数)(原始宽度*百分比);
新高度=(整数)(原始高度*百分比);
}
其他的
{
newWidth=大小。宽度;
newHeight=大小。高度;
}
Image newImage=新位图(newWidth、newHeight);
使用(Graphics graphicsHandle=Graphics.FromImage(newImage))
{
graphicsHandle.InterpolationMode=InterpolationMode.HighQualityBicubic;//最近的邻居
graphicsHandle.DrawImage(图像,0,0,新宽度,新高度);
}
返回(newImage);
}
…我从这个函数得到的图像是32ARgb图像,这不是我想要的。因为它引入了3个额外的颜色通道(Alpha和RBG)。 有没有一种方法可以在不改变颜色模式的情况下缩小图像

我还尝试使用InterpolationMode.NearestNeighbork,但只得到了一个32位位图

提前表示感谢和问候
Robert

你是指位图(整数高度、整数大小、像素格式)?如何在里面复制旧照片?我是否必须使用Bitmap.clone()?我还考虑过在游戏中直接使用构造函数来定义,但是我会得到一个错误:grafic对象无法从具有索引像素格式的pitcure创建。我还尝试了以下行:var newBmp=ConvertedImage.clone(新矩形(0,0,ConvertedImage.Width,ConvertedImage.Height),PixelFormat.Format8Bppined);我必须从原始图像复制调色板并在新图像上重新绘制吗?最终我想要的是1024x768(Format8Bppined)->500x375(Format8Bppined)。现在代码会调整大小,但它会输出一个32Argb(带有4个8比特的通道)。但我想保留8bppindex的调色板。我怎样才能做到这一点?我想我必须把4*8比特缩小到8比特,还是有更好的办法?或者我可以从大的8bpp图像中保存调色板并将其分配给32Argb位图吗?您是指位图(int-height、int-size、Pixelformat和Pixelformat)?如何在里面复制旧照片?我是否必须使用Bitmap.clone()?我还考虑过在游戏中直接使用构造函数来定义,但是我会得到一个错误:grafic对象无法从具有索引像素格式的pitcure创建。我还尝试了以下行:var newBmp=ConvertedImage.clone(新矩形(0,0,ConvertedImage.Width,ConvertedImage.Height),PixelFormat.Format8Bppined);我必须从原始图像复制调色板并在新图像上重新绘制吗?最终我想要的是1024x768(Format8Bppined)->500x375(Format8Bppined)。现在代码会调整大小,但它会输出一个32Argb(带有4个8比特的通道)。但我想保留8bppindex的调色板。我怎样才能做到这一点?我想我必须把4*8比特缩小到8比特,还是有更好的办法?或者我可以从大的8bpp图像中保存颜色调色板并将其分配给32Argb位图吗?