当图像未存储在本地文件夹中时,更改像素空引用异常

本文关键字:像素 异常 引用 存储 图像 文件夹 | 更新日期: 2023-09-27 18:27:01

我正在尝试将图像的像素更改为红色,图像是从服务器下载的,不存储在本地文件夹中。下面的代码我正在使用,当图像存储在本地文件夹中时,它工作得非常好。

    private ImageSource changeimagepixel(string p)
    {
        Uri uri = new Uri(p, UriKind.RelativeOrAbsolute);
        ImageSource imgsource = new BitmapImage(uri);
        image.Source = imgsource;
        WriteableBitmap image1 = new WriteableBitmap((BitmapSource)image.Source);
        WriteableBitmap image2 = ChangeColor(image1);
        image.Source = image2;
        return image.Source;
    }
    public static WriteableBitmap ChangeColor(WriteableBitmap scrBitmap)
    {
        //You can change your new color here. Red,Green,LawnGreen any..
        Color newColor =Colors.Red;
        Color actulaColor;
        //make an empty bitmap the same size as scrBitmap
        WriteableBitmap newBitmap = new WriteableBitmap(scrBitmap.PixelWidth, scrBitmap.PixelHeight);
        for (int i = 0; i < scrBitmap.PixelWidth; i++)
        {
            for (int j = 0; j < scrBitmap.PixelHeight; j++)
            {
                //get the pixel from the scrBitmap image
                actulaColor = scrBitmap.GetPixel(i, j);
                // > 150 because.. Images edges can be of low pixel colr. if we set all pixel color to new then there will be no smoothness left.
                if (actulaColor.A > 150)
                    newBitmap.SetPixel(i, j, (Color)newColor);
                else
                    newBitmap.SetPixel(i, j, actulaColor);
            }
        }
        return newBitmap;
    }

这里,p是从服务器获得的图像的URL,并且我在上得到NullReference异常

WriteableBitmap image1 = new WriteableBitmap((BitmapSource)image.Source);

请任何人提出解决方案,更改不在本地文件夹中并从服务器获取的图像的像素。

这将是一个很大的帮助。

谢谢。

当图像未存储在本地文件夹中时,更改像素空引用异常

在对其进行任何操作之前,您需要等待它完全下载。

示例:

<Grid x:Name="ContentPanel">
    <Image x:Name="myImage"></Image>
</Grid>
public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        // download my dogecoin image
        myImage.Source = new BitmapImage(new Uri("http://www.chubosaurus.com/dogecoin_wp8.jpg", UriKind.Absolute));           
        // hook the download complete 
        myImage.ImageOpened += bi_ImageOpened;
    }
    // image has downloaded completely, do your actions afterwards
    void bi_ImageOpened(object sender, RoutedEventArgs e)
    {
        // ..... your code
        // put a break point here, you will see it will break when the image has downloaded
    }
 }

如果事件没有触发——很可能是因为您动态创建了Image,而没有将其添加到可视化树中,所以要解决此问题,请执行类似的操作

<!-- the default ContentPanel when creating the project -->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <!--- your content -->
</Grid>
// Constructor
public MainPage()
{
    InitializeComponent();

    Image mytestimage = new Image();
    mytestimage.Visibility = System.Windows.Visibility.Collapsed;
    mytestimage.Source = new BitmapImage(new Uri("http://www.chubosaurus.com/dogecoin_wp8.jpg", UriKind.Absolute));           
    // add it to the Visual Tree as a child of ContentPanel
    this.ContentPanel.Children.Add(mytestimage);
    // hook the download complete
    mytestimage.ImageOpened += mytestimage_ImageOpened;            
}
// image has been downloaded successfully
void mytestimage_ImageOpened(object sender, RoutedEventArgs e)
{            
    // your code
}