关于Image.FromStream()变换图片产生的问题

本文探讨了使用C#进行图像处理时遇到的问题,特别是通过Image.FromStream加载图像并进行旋转操作时出现的内存不足警告。文章提供了具体代码示例,并对比了Image.FromFile方法的不同表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近学习C#编程,想自己编些桌面小程序玩玩,接触到了线程,GDI+,网络文件传输一系列的东东。编这些程序前我已经知道这水很深,可是接触了,才发现这水这么这么深,会淹死人的……几乎每天都是从早到晚的盯着MSDN,这个连接跳到那个连接,那个连接又跳回这个连接,那个寒啊……

言归正传,在编程的时候发现了一个问题,关于绘制Image.FromStream()所产生的图片时候会出现内存不足的警告。而Image.FromFile()着根本不会出现这问题,实在是很不解啊。

现将代码公布如下,给大家参考,希望大家可以告诉我,这是为什么。。。难道这就是传说中的BUG~~我运气也太好了点呀。。。。微软是不是要发点奖金给我。。呵呵。

None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.ComponentModel;
None.gif
using System.Data;
None.gif
using System.Drawing;
None.gif
using System.Text;
None.gif
using System.Windows.Forms;
None.gif
using System.Drawing.Drawing2D;
None.gif
using System.IO;
None.gif
None.gif
namespace Project1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public partial class Clock : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//定义一个Image
InBlock.gif
        Image image1;
InBlock.gif        
//旋转角度,值为90和180的时候绘制会产生内存不足警告
InBlock.gif
        float float1 = 88;
InBlock.gif
InBlock.gif        
//定义128*128图象的初始点
ExpandedSubBlockStart.gifContractedSubBlock.gif
        Point[] PT = new Point[] dot.gifnew Point(-64-64), new Point(64-64), new Point(-6464) };
InBlock.gif
InBlock.gif        
public Clock()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Refresh();
InBlock.gif            float1
++;
InBlock.gif            label1.Text 
= float1.ToString();
InBlock.gif            System.Drawing.Graphics PicGraphics 
= this.pictureBox1.CreateGraphics();
InBlock.gif            
//把64,64点设置为中心点,将以这个中心点为圆心旋转
InBlock.gif
            PicGraphics.TranslateTransform(6464);
InBlock.gif            GraphicsState gs 
= PicGraphics.Save();
InBlock.gif            PicGraphics.RotateTransform(float1);
InBlock.gif            PicGraphics.DrawImage(image1, PT);
InBlock.gif            PicGraphics.Restore(gs);
InBlock.gif            PicGraphics.Dispose();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Clock_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            label1.Text 
= float1.ToString();
InBlock.gif
InBlock.gif            
//读取一个128*128的PNG文件
InBlock.gif
            byte[] nbytes = new byte[(new FileInfo("a.png")).Length];
InBlock.gif            
using (FileStream fs = new FileStream("a.png", FileMode.Open, FileAccess.Read, FileShare.Read, 1true))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                fs.Read(nbytes, 
0, nbytes.Length);
InBlock.gif                
using (Stream stream = new MemoryStream(nbytes))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    image1 
= Image.FromStream(stream);
InBlock.gif
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private System.Windows.Forms.Button button1;
InBlock.gif        
private System.Windows.Forms.PictureBox pictureBox1;
InBlock.gif        
private System.Windows.Forms.Label label1;
InBlock.gif
InBlock.gif        
private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.button1 = new System.Windows.Forms.Button();
InBlock.gif            
this.pictureBox1 = new System.Windows.Forms.PictureBox();
InBlock.gif            
this.label1 = new System.Windows.Forms.Label();
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.pictureBox1)).BeginInit();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// button1
InBlock.gif            
// 
InBlock.gif
            this.button1.Location = new System.Drawing.Point(1212);
InBlock.gif            
this.button1.Name = "button1";
InBlock.gif            
this.button1.Size = new System.Drawing.Size(7523);
InBlock.gif            
this.button1.TabIndex = 0;
InBlock.gif            
this.button1.Text = "增加1角度";
InBlock.gif            
this.button1.UseVisualStyleBackColor = true;
InBlock.gif            
this.button1.Click += new System.EventHandler(this.button1_Click);
InBlock.gif            
// 
InBlock.gif            
// pictureBox1
InBlock.gif            
// 
InBlock.gif
            this.pictureBox1.Location = new System.Drawing.Point(7365);
InBlock.gif            
this.pictureBox1.Name = "pictureBox1";
InBlock.gif            
this.pictureBox1.Size = new System.Drawing.Size(128128);
InBlock.gif            
this.pictureBox1.TabIndex = 1;
InBlock.gif            
this.pictureBox1.TabStop = false;
InBlock.gif            
// 
InBlock.gif            
// label1
InBlock.gif            
// 
InBlock.gif
            this.label1.AutoSize = true;
InBlock.gif            
this.label1.Location = new System.Drawing.Point(12522);
InBlock.gif            
this.label1.Name = "label1";
InBlock.gif            
this.label1.Size = new System.Drawing.Size(1712);
InBlock.gif            
this.label1.TabIndex = 2;
InBlock.gif            
InBlock.gif            
// 
InBlock.gif            
// Clock
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
InBlock.gif            
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
InBlock.gif            
this.ClientSize = new System.Drawing.Size(292266);
InBlock.gif            
this.Controls.Add(this.label1);
InBlock.gif            
this.Controls.Add(this.pictureBox1);
InBlock.gif            
this.Controls.Add(this.button1);
InBlock.gif            
this.Name = "Clock";
InBlock.gif            
this.Text = "Clock";
InBlock.gif            
this.Load += new System.EventHandler(this.Clock_Load);
InBlock.gif            ((System.ComponentModel.ISupportInitialize)(
this.pictureBox1)).EndInit();
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif            
this.PerformLayout();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
static class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>
InBlock.gif            
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif            
/// </summary>

InBlock.gif            [STAThread]
InBlock.gif            
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Application.EnableVisualStyles();
InBlock.gif                Application.SetCompatibleTextRenderingDefault(
false);
InBlock.gif                Application.Run(
new Clock());
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/linyu0423/archive/2007/06/04/770977.html

private String extractImageText(MultipartFile file) { try (InputStream is = file.getInputStream()) { BufferedImage image = ImageIO.read(is); if (image == null) { return MessageUtils.message("Image.parsing.failed"); } // 1. 图片预处理 BufferedImage processedImage = preprocessImage(image); // 2. OCR识别 String result = tesseract.doOCR(processedImage); // result = result.replaceAll("\\s+", " ").trim(); System.out.println("图片内容:\n" + result); return result; } catch (Exception e) { e.printStackTrace(); // 打印异常方便排查 return MessageUtils.message("file.read.picture.error"); } } // 启用预处理 private BufferedImage preprocessImage(BufferedImage image) { // 灰度化 BufferedImage gray = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY); // 直方图均衡化 float[] kernelData = { -1, -1, -1, -1, 9, -1, -1, -1, -1 }; Kernel kernel = new Kernel(3, 3, kernelData); ConvolveOp convolveOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); BufferedImage enhanced = convolveOp.filter(gray, null); // 自适应二值化 BufferedImage binary = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY); return binary; } @PostConstruct public void initOcrEngine() { tesseract = new Tesseract(); //语言包路径和支持语言 tesseract.setDatapath("D:\\maven_use\\lingxi-lhc\\lingxi-ai-extend\\lingxi-ai-comparison\\src\\main\\resources\\tessdata"); tesseract.setLanguage("eng+chi_sim"); // tesseract.setPageSegMode(6); // 自动页面分割 // tesseract.setOcrEngineMode(1); // LSTM引擎 // 关键参数调整 tesseract.setPageSegMode(4); // 假设文本为垂直排列单列 tesseract.setOcrEngineMode(3); // 使用默认OCR引擎(更擅长结构化数据) tesseract.setVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/\\-|().,"); tesseract.setVariable("preserve_interword_spaces", "1"); tesseract.setVariable("textord_debug_tabfind", "1"); // 调试表格识别 }这样
最新发布
07-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值