ITPUB??ì3
ITPUB论坛 » Web开发 » ASP.NET与AJAX » C#图像提取


标题: C#图像提取
离线 juanpeng
中级会员



精华贴数 0
个人空间 0
技术积分 4048 (343)
社区积分 0 (1438955)
注册日期 2007-6-21
论坛徽章:27
开发板块每日发贴之星开发板块每日发贴之星开发板块每日发贴之星生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠
生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠

发表于 2008-8-4 21:46 
C#图像提取

Bitmap JT = (Bitmap)Bitmap.FromFile("E:\1.bmp");
pictureBox1.Image = JT;
Rectangle xtrect = new Rectangle(241, 214, 165, 125);
Bitmap JTxt = JT.Clone(xtrect,System.Drawing.Imaging.PixelFormat.Canonical);
pictureBox1.Image = JTxt;

运行这段代码到第四句的时候就会提示说内存不足。。图片不到一M。。这得怎么解决???


只看该作者    顶部
离线 juan002
中级会员



精华贴数 0
个人空间 0
技术积分 3610 (394)
社区积分 0 (1438956)
注册日期 2007-6-21
论坛徽章:19
开发板块每日发贴之星生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠
生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠

发表于 2008-8-4 21:46 
C# code
//提供一段代码,供LZ参考
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;

namespace LandaControl8_sqlserverImage
{
    /// <summary>
    /// 对图像进行一系列处理
    /// </summary>
    public class TreatImage
    {
        #region Bitmap转换byte[]数组
        /// <summary>
        /// Bitmap转换byte[]数组
        /// </summary>
        /// <param name="bmp"></param>
        /// <returns></returns>
        public static byte[] Bmptobyte(Bitmap bmp)
        {
            MemoryStream ms = new MemoryStream();
            bmp.Save(ms, ImageFormat.Jpeg);
            ms.Flush();
            byte[] buffer = ms.GetBuffer();
            ms.Close();
            return buffer;
        }
        #endregion

        #region byte[]数组转换Bitmap
        /// <summary>
        /// byte[]数组转换Bitmap
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static Bitmap bytetobmp(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream();
            ms.Write(buffer, 0, buffer.Length);
            Bitmap bmp = new Bitmap(ms);
            ms.Close();
            return bmp;
        }
        #endregion

        #region 选取本地图片
        /// <summary>
        /// 选取本地图片
        /// </summary>
        /// <param name="IMG"></param>
        /// <returns></returns>
        public static Bitmap LocalIMG(string IMG)
        {
            FileStream fs = new FileStream(IMG, FileMode.Open);
            Bitmap bmp = new Bitmap(fs);
            fs.Close();
            return bmp;
        }
        #endregion

        #region 返回流状态图片
        /// <summary>
        /// 返回流状态图片
        /// </summary>
        /// <param name="Img"></param>
        /// <returns></returns>
        public static Bitmap ImgFromBase64(string Img)
        {
            Bitmap bmp;
            byte[] buffer = Convert.FromBase64String(Img);
            if (buffer.Length > 0)
            {
                MemoryStream ms = new MemoryStream();
                ms.Write(buffer, 0, buffer.Length);
                bmp = new Bitmap(ms);
                ms.Close();
                return bmp;
            }
            else
            {
                bmp = DefaultPic();
                return bmp;
            }
        }
        #endregion

        #region 默认图片
        /// <summary>
        /// 默认图片
        /// </summary>
        /// <returns></returns>
        public static Bitmap DefaultPic()
        {
            FileStream fs = new FileStream(Application.StartupPath + @"Goodr.jpg", FileMode.Open);
            Bitmap bmp = new Bitmap(fs);
            fs.Close();
            return bmp;
        }
        #endregion

        private void numericUpDown2_ValueChanged(object sender, System.EventArgs e)
        {//更新图像显示   
            //this.button2.Refresh();
        }
        #endregion

        #region 尺寸枚举
        public enum Dimensions
        {
            Width,
            Height
        }
        #endregion

        #region 锚位置枚举
        public enum AnchorPosition
        {
            Top,
            Center,
            Bottom,
            Left,
            Right
        }
        #endregion

        #region 按比例转换图像
        /// <summary>
        /// 返回按比价转换后的图像
        /// </summary>
        /// <param name="imgPhoto">指定图像</param>
        /// <param name="Size">指定尺寸</param>
        /// <param name="Dimension">按宽度或高度</param>
        /// <returns>返回按比价转换后的图像</returns>
        public static Image ConstrainProportions(Image imgPhoto, int Size, Dimensions Dimension)
        {
            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            int sourceX = 0;
            int sourceY = 0;
            int destX = 0;
            int destY = 0;
            float nPercent = 0;

            switch (Dimension)
            {
                case Dimensions.Width:
                    nPercent = ((float)Size / (float)sourceWidth);
                    break;
                default:
                    nPercent = ((float)Size / (float)sourceHeight);
                    break;
            }

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

            grPhoto.DrawImage(imgPhoto,
            new Rectangle(destX, destY, destWidth, destHeight),
            new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
            GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return bmPhoto;
        }
        #endregion

        #region 按固定尺寸转换图像
        /// <summary>
        /// 返回按固定尺寸转换的图像
        /// </summary>
        /// <param name="imgPhoto">指定图像</param>
        /// <param name="Width">生成后图像的宽度</param>
        /// <param name="Height">生成后图像的高度</param>
        /// <returns>返回按固定尺寸转换的图像</returns>
        public static Image FixedSize(Image imgPhoto, int Width, int Height)
        {
            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            int sourceX = 0;
            int sourceY = 0;
            int destX = 0;
            int destY = 0;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)Width / (float)sourceWidth);
            nPercentH = ((float)Height / (float)sourceHeight);

            //if we have to pad the height pad both the top and the bottom
            //with the difference between the scaled height and the desired height
            if (nPercentH < nPercentW)
            {
                nPercent = nPercentH;
                destX = (int)((Width - (sourceWidth * nPercent)) / 2);
            }
            else
            {
                nPercent = nPercentW;
                destY = (int)((Height - (sourceHeight * nPercent)) / 2);
            }

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.Clear(Color.Red);
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

            grPhoto.DrawImage(imgPhoto,
                new Rectangle(destX, destY, destWidth, destHeight),
                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return bmPhoto;
        }
        #endregion

            }
}


只看该作者    顶部
离线 dotnetworker
一般会员



精华贴数 0
个人空间 0
技术积分 3866 (359)
社区积分 0 (1439940)
注册日期 2007-6-21
论坛徽章:33
开发板块每日发贴之星开发板块每日发贴之星开发板块每日发贴之星生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠
生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠

发表于 2008-8-4 21:46 
内存不足,可能是你的某些参数设置不对!


只看该作者    顶部
 
    

相关内容


CopyRight 1999-2006 itpub.net All Right Reserved.
北京皓辰广域网络信息技术有限公司. 版权所有
E-mail:Webmaster@itpub.net
京ICP证:010037号 联系我们 法律顾问