亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何加載圖像并從任何地方訪問它?

如何加載圖像并從任何地方訪問它?

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 關注
  • 154 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號