且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C#中将图像转换为WPF中的位图图像

更新时间:2023-01-23 10:15:41


如果您有一个字节数组表示WPF可以解码的文件(bmp,jpg,gif,png,tif,ico),则可以执行以下操作
Hi,
If you have a byte array that represents a file that WPF can decode (bmp, jpg, gif, png, tif, ico), you can do the following
BitmapSource LoadImage(Byte[] imageData)
{
    using (MemoryStream ms = new MemoryStream(imageData))
    {
        var decoder = BitmapDecoder.Create(ms,
            BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
        return decoder.Frames[0];
    }
}


以下文章包含有关将ImageSource转换为BitmapImage和Bitmap的有用信息:
http://***.com/questions/920517/convert-imagesource-to-bitmapimage-wpf

http://***.com/questions/4506692/how-to-convert-imagesource-到位图

这里还有一个使用第三方工具包的解决方案:
http://support.leadtools.com/CS/forums/41168/ShowPost.aspx
The following articles contain useful information about converting ImageSource to BitmapImage and Bitmap:
http://***.com/questions/920517/convert-imagesource-to-bitmapimage-wpf

http://***.com/questions/4506692/how-to-convert-imagesource-to-bitmap

There''s also a solution that uses a third party toolkit here:
http://support.leadtools.com/CS/forums/41168/ShowPost.aspx


电话:富文本框
单击按钮时,我正在使用富文本框创建一个位图,然后将其置于与Telep相同的位置,但在其上方.然后我终于使它成为水平镜像.

Telep: a rich text box
On a button click, I was taking the rich text box creating a bitmap, and then making it in the same location as telep, but above it. And then I finally made it mirrored horizontally.

public void pictureBox6_Click(object sender, EventArgs e)
        {            
            //Create a PictureBox with the same location and size as the RichTextBox.
            PictureBox pic = new PictureBox();
            pic.Location = this.telep.Location;
            pic.Size = this.telep.Size;

            //Draw the richTextBox to a bitmap
            Bitmap bitmap = new Bitmap(this.telep.Width, this.telep.Height);
            DrawToBitmap(this.telep, bitmap, new Rectangle(Point.Empty, this.telep.Size));
            bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX);

            //Set the image of the PictureBox to that image.
            pic.Image = bitmap;
            this.Controls.Add(pic);
            pic.BringToFront();

            pic.Dock = DockStyle.Fill;
        }

        private void DrawToBitmap(RichTextBox richTextBox, Bitmap image, Rectangle targetRectangle)
        {
            
            Graphics g = telep.CreateGraphics();
            Graphics g2 = Graphics.FromImage(image);
            IntPtr gi = g.GetHdc();
            IntPtr gi2 = g2.GetHdc();
            BitBlt(gi2, 0, 0, richTextBox.Width, richTextBox.Height, gi, 0, 0, 0x00CC0020);
            g.ReleaseHdc();
            g2.ReleaseHdc();
            g.Dispose();
            g2.Dispose();

        }