为了账号安全,请及时绑定邮箱和手机立即绑定

如何加载图像并从任何地方访问它?

如何加载图像并从任何地方访问它?

C#
慕标琳琳 2023-07-09 17:26:56
我想从表单窗口浏览图像。我还创建了一个类并创建了一些过滤器。我可以从表格中读取这张图片。我的目标是在课堂上宣布它。并在任何地方使用这个图像。但我不知道我该怎么做。private void btn_BROWSE_Click(object sender, EventArgs e){    OpenFileDialog imge = new OpenFileDialog();     imge.Filter = "Extensions |*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff|"                  + "BMP|*.bmp|GIF|*.gif|JPG|*.jpg;*.jpeg|PNG|*.png|TIFF|*.tif;*.tiff|"                  + "Zip Files|*.zip;*.rar";    imge.ShowDialog();     string imgepath = imge.FileName;    pBox_SOURCE.ImageLocation = imgepath;//i'm browsing an image}private void sliderKernel_MouseUp(object sender, MouseEventArgs e){    Bitmap OriginalImage = new Bitmap(pBox_SOURCE.Image);} class Filters{     // (i would like to initialize my image in here not in form :) ) }
查看完整描述

2 回答

?
慕运维8079593

TA贡献1876条经验 获得超5个赞

我将定义一个抽象类 Filter 并将每个过滤器实现为该类的继承人。


public abstract class Filter

{   

    public Bitmap Image { get; set; }


    public abstract void Apply();

}

一个实现是:


public class SliderKernel : Filter

{   

    public overrides void Apply()

    {

        //manipulates the Image property

    }

}

如果您想在任何地方使用该图像,您应该将其声明为类的静态成员:


public static class ImageContainer

{

     public static Bitmap Image { get; set; }

}

您可以在表单代码中使用所有这些,如下所示:


private void btn_BROWSE_Click(object sender, EventArgs e)

{

    OpenFileDialog imge = new OpenFileDialog(); 

    imge.Filter = "Extensions |*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff|"

                  + "BMP|*.bmp|GIF|*.gif|JPG|*.jpg;*.jpeg|PNG|*.png|TIFF|*.tif;*.tiff|"

                  + "Zip Files|*.zip;*.rar";

    imge.ShowDialog(); 

    string imgepath = imge.FileName;

    pBox_SOURCE.ImageLocation = imgepath;//i'm browsing an image


    //save the image to the container

    ImageContainer.Image = new Bitmap(pBox_SOURCE.Image);

}


private void sliderKernel_MouseUp(object sender, MouseEventArgs e)

{

    Filter filter = new SliderKernel () { Image = ImageContainer.Image };

    filter.Apply();


查看完整回答
反对 回复 2023-07-09
?
慕侠2389804

TA贡献1719条经验 获得超6个赞

我认为你应该将图像转换为字节数组


使用以下代码并将其存储在静态类中


public byte[] ImageToByteArray(System.Drawing.Image imageIn)

{

   using (var ms = new MemoryStream())

   {

      imageIn.Save(ms,imageIn.RawFormat);

      return  ms.ToArray();

   }

}

https://www.codeproject.com/Articles/15460/C-Image-to-Byte-Array-and-Byte-Array-to-Image-Conv


并使用此代码转为图形显示在pictureBox中


public Image byteArrayToImage(byte[] byteArrayIn)

{

     MemoryStream ms = new MemoryStream(byteArrayIn);

     Image returnImage = Image.FromStream(ms);

     return returnImage;

}


查看完整回答
反对 回复 2023-07-09
  • 2 回答
  • 0 关注
  • 86 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信